c - fscanf 不会工作两次?

标签 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); 就在它用于 while 循环之前。我以为 fclose 应该关闭文件并使其无法使用?无论如何都有效,但我真正想要的是使用 文件的一个实例 来读取其中的所有字符和字符串。

最佳答案

做如下的事情——当然是懒惰的方式

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;

关于c - fscanf 不会工作两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43638832/

相关文章:

c++ - 如何检测加密的文件?

c - 在 apache2 中重新编译 mod\module

c - 指向多个源文件之间共享的数组的指针

c - 如何使用套接字检查打开的端口

c - 什么 api 可用于创建到 ext2/3 上现有 inode 的硬链接(hard link)

c - 通过引用传递并将值赋给结构指针的指针

c++ - 当 API 请求函数指针时使用函数对象

c - 中断导致运行时错误

c - 警告 : Comparison between pointer and integer in while loop

c - 在C中获取二进制目录