It works for one or the other, but not both. If I put in both sections of code (character count and word count) it will have the word count return 0 to the counter. If I take one away, they work fine! I will put it in separate functions when im done..any ideas of what is wrong??
here is the code
Code:
FILE *fp;
while (1){ char filename[100];
puts("Enter the name of the file to open");
scanf("%s", &filename);
fp=fopen(filename, "r");
if (fp == 0)
{
printf("\n\n%s file name not found!\n\n", filename);
}
else
{
puts("File successfully opened");
break;
}
}//End while loop
//////////////////////////////////////
//charcount function
while (1) {
ch = fgetc(fp);
++charCount;
if (ch == -1)
break;
}
ch = 0;
printf("\nNumber of characters: %i\n", charCount);
//////////////////////////////////////////////////////
//WORDS COUNTER FUNCTION
while((i = getc(fp)) != EOF)
{
if ((i = getc(fp)) == ' ')
++wCount;
}
printf("Number of words: %i\n", wCount);