Cerca di utilizzare sempre i valori di ritorno. Guarda qui: fscanf
codice:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
FILE* fp = NULL;
int characters = 0;
int words = 0;
int lines = 0;
int retv;
char buf = 0;
fp = fopen(argv[1], "rb");
while (!feof(fp) && (retv = fscanf(fp, "%c", &buf)) != EOF)
{
characters++;
if (buf == ' ') words++;
if (buf == '\n')
{
words++;
lines++;
}
}
printf("%d %d %d\n", characters, words, lines);
return 0;
}
Edit: Puoi anche prima leggere e poi testare la fine del file
codice:
while ((retv = fscanf(fp, "%c", &buf)) && !feof(fp))
// oppure
//while ((retv = fscanf(fp, "%c", &buf)), !feof(fp))
{
...
}