I'm just trying to get fscanf to read all the characters in a file, along with all the words, but whenever I try to run a while
loop on the file I opened twice it doesn't seem to work? It only seems to be able to use fscanf
on a file one time. I found a work around where I can scan the same file twice except I need to open the file a 2nd time for this to work. How can I use fscanf
on the same instance of a file twice?
/*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;
}
As you can see i have to create two instances of the file or else it will not work. The first instance is called wordFile
and the 2nd instance is called charFile
. Here's the thing though: Both loops work, it's just that I can't use them on the same file twice. How can I make it so that I will only need to open one instance of the file and then use it to count both the words and the chars in it?
Things I tried: adding the space as suggested here didn't work: C: Multiple scanf's, when I enter in a value for one scanf it skips the second scanf (i searched fscanf but scanf is all that came up so i went off of that).
Another work around that I found strange: if i use wordFile
in the 2nd while loop to search for chars it works, the only problem is I have to declare fclose(wordFile);
right before it's used in the while
loop. I thought fclose
was suppose to close the file and make it unusable? Anyways that worked but what I really want is to use use one instance of the file to read all the chars and strings in it.