gpt4 book ai didi

c - fscanf 不会工作两次?

转载 作者:太空宇宙 更新时间:2023-11-04 03:23:02 24 4
gpt4 key购买 nike

我只是想让 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com