Ok made a booboo with EOF. It can't look for EOF in data. Thats big faq to memorize everything, but thanks for showing me. The only prolem I have left is when I do:
Code:
[academ] $ wc d5.dat
13 16 115 d5.dat
and then a run my executable, I get 13 lines, but only 102 characters. Should I be using something else instead of isprint?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main () {
char buf[11]; /* file buffer area */
int lines, chars, x; /* count of lines and characters */
FILE *in; /* input file stream pointer */
lines = 0;
chars = 0;
if ((in = fopen("d5.dat", "r")) == NULL) {
perror("fopen: d5.dat");
exit(1);
}
while(fgets(buf, 11, in) != NULL) {
/* fgets(buf, 11, in); */
for(x = 0; buf[x] != '\0'; x++)
if(buf[x] == '\n')
lines++;
for(x = 0; buf[x] != '\0'; x++)
if(isprint(buf[x]))
chars++;
}
fclose(in);
printf("\nLines:%d\nCharacters:%d\n\n", lines, chars);
printf ("Press ENTER to continue.\n");
getchar ();
return 0;
}