fscanf不会工作两次?

时间:2017-04-26 15:42:18

标签: c

我只是想让fscanf读取文件中的所有字符以及所有单词,但每当我尝试在文件上运行while循环时,我打开两次它似乎不会上班?它似乎只能在一次文件上使用fscanf。我找到了一个可以扫描同一文件两次的工作,除了我需要打开文件第二次才能使用。如何在同一个文件实例上使用fscanf两次?

/*Description: Program will open a file named Story.txt
           then counts the number of words and characters
           and prints them out */

int main(){

char word[225]; // will be used to hold words (array of chars)
char c; // will be used to hold chars
int wordCount = 0; // will be used to hold the number of words in the file
int charCount = 0; // will be used to hold the number of chars in the file

FILE* wordFile = fopen("Story.txt","r"); // opens the file Story.txt for counting words

printf("Words: "); // indicates that the following outout will be the words of the file

while(fscanf(wordFile,"%s",&word)==1){ // a loop to scan the file for all the words in it until the end of the file
    printf(" %s ",word); // prints out the word in the given cycle
    wordCount = wordCount + 1; // keeps count of the words the loop has scanned up till now
}

fclose(wordFile); // closes wordFile and frees the memory
FILE* charFile = fopen("Story.txt","r"); // opens file Story.txt for counting chars

printf("\n\nChars: "); // indicates the following output will be the chars of the file

while(fscanf(charFile,"%c",&c)==1){ // a loop to scan the file for all the chars in it until the end fot he file
    if(c!=' '){ // will check if the char is a space, if it is it will not count it
        printf(" %c ",c); // prints out the char for the given cycle
        charCount = charCount + 1; // keeps count of the chars the loop has scanned up till now
    }
}

fclose(charFile); // closes charFile and frees the memory

printf("\n\nWord count: %d and Char count: %d",wordCount,charCount-1); // prints out the word count and char count

return 0;
}

正如您所看到的,我必须创建文件的两个实例,否则它将无效。第一个实例称为wordFile,第二个实例称为charFile。但事情是这样的:两个循环都可以工作,只是我不能在同一个文件两次上使用它们。我该如何制作它以便我只需要打开文件的一个实例然后用它来计算其中的单词和字符?

我尝试过的事情:按照此处的建议添加空间不起作用:C: Multiple scanf's, when I enter in a value for one scanf it skips the second scanf(我搜索了fscanf,但是scanf就是所有出现的,所以我离开了它。)

另一项解决方法我发现很奇怪:如果我在第二个while循环中使用wordFile来搜索字符,那么唯一的问题是我必须在<}之前声明fclose(wordFile); / strong>它在while循环中使用。我以为fclose假设关闭文件并使其无法使用?无论如何,但我真正想要的是使用文件的一个实例来读取其中的所有字符和字符串。

2 个答案:

答案 0 :(得分:1)

做类似下面的事情 - 当然是懒惰的方式

char word[225]; 
char c; 
int wordCount = 0; 
int charCount = 0; 

FILE* wordFile = fopen("Story.txt","r"); 

printf("Words: "); 

while(fscanf(wordFile,"%s",word)==1){  // word gives the address not &word
    wordCount = wordCount + 1; 
}

printf("%d\n",wordCount);

fseek(wordFile,0,SEEK_SET); // setting the file pointer to point to the beginning of file

printf("Chars: "); 

while(fscanf(wordFile,"%c",&c)==1){ 
    if(c!=' '&& c!='\n' && c!='\t'){ 
        charCount = charCount + 1; 
    }
}
printf("%d\n",charCount);
fclose(wordFile);  // Closing the file once for all

return 0;

答案 1 :(得分:0)

fscanf(wordFile,"%s",&word);

变量&#34; word&#34;是指向数组的第一个元素(字符串)的指针,所以在这里扫描地址而不是值。你应该使用:

fscanf(wordFile,"%s",word);