Basically I'm writing a program that reads in the number of words in a file and counts them based on the number of whitespaces, though I'm also considering using tokenising im just more familiar with whitespaces.
It compares the ACII code of whitespace(I think) then adds one due to not being able to read past the EOF marker. I havent quite got the code there yet though and I'm stumped, so any help is appreciated.
#include <stdio.h>
int main()
{
FILE * fptr;
if((fptr = fopen("words.txt", "r")) == NULL)
{
printf("Cannot open the file: input.txt\n");
}
else
{
printf("File opened successfully for reading\n");
}
int i;
int word_number = 0;
int total_words = 0;
char myChar;
char string[60];
fptr = fopen("words.txt", "r");
while(fgets(string, 60, fptr))
{
for(i=0; i>0; i++)
{
if(myChar == 0127)
{
word_number++;
}
total_words = word_number+1;
}
}
printf("The number of words is: %d \n", total_words);
fclose(fptr);
}