2016-11-29 12 views
0

レポートファイルには、次の文字列を含める必要があります。 1.単語数 2大文字は小文字の 3.数桁の 4.数テキストファイルの内容を読み込み、大文字/小文字の文字数と数字を別々のファイルに書き込むプログラムを作成します。

私は正常にファイルを読み、言葉の文字と数字を数えたが、私は新しいファイルに内容を書き込んで問題が生じています、任意のヘルプは次のようになりますしています感謝。

#include <stdio.h> 
#include <ctype.h> 
#define SIZE 40 

int main(void) 
{ 
char ch, filename[SIZE]; 
int digits = 0; 
int upper = 0; 
int lower = 0; 
int entered = 0; 
int words = 0; 
unsigned long count = 0; 
FILE *fp; 
printf("Please enter the filename to read: "); 
gets(filename); 
// "r" reads the file fopen opens the file 
if ((fp = fopen(filename, "r")) == NULL) 
{ 
    printf("Cannot open the file, %s\n", filename); 
} 
else 
{ 
    puts("Successfully opened, now reading.\n"); 

    while ((ch=getc(fp)) != EOF) 
    { 
     if (isalnum(ch)) 
     { 
      if(!entered) 
      { 
       entered = 1; 
       words++; 
      } 

     } 

     else 
     { 
      if (entered) 
      { 
       entered = 0; 
      } 
     } 

     if (isupper(ch)) 
      { 
      upper++; 
      } 

     else if (islower(ch)) 
      { 
       lower++; 
      } 
     else if (isdigit(ch)) 
     { 
      digits++; 
     } 

    } 

    } 

    fclose(fp); //make sure to close the file if you open one 

    char filename2 [SIZE]; 
    FILE *fp2; 

fprintf(stdout, "Please enter the file name to write in: "); 
gets(filename2); 

if ((fp2 = fopen("filename2", "w")) == NULL) 
{ 
    printf("Cannot create the file, %s\n", filename2); 
} 
else 
{ 
    fprintf(fp2, "The file \"%s\" has %lu Words.\n", filename, words); 
    fprintf(fp2, "The file \"%s\" has %lu Digits.\n", filename, digits); 
    fprintf(fp2, "The file \"%s\" has %lu upper case letters.\n", filename, upper); 
    fprintf(fp2, "The file \"%s\" has %lu lower case letters.\n", filename, lower); 

} 

fclose(fp2); 


return 0; 

}

答えて

2

代わりの

if ((fp2 = fopen("filename2", "w")) == NULL) 

書き込み

その後
if ((fp2 = fopen(filename2, "w")) == NULL) 

、自分自身を蹴ります。

+0

ワウありがとう、私の愚かさを赦してください。 –

関連する問題