-
stop read in newline
hi all, i'm so mess up w/ this pro.
suppose a data file File.dat:
a b c d e
f g
how can i stop read those character when matching the newline?
note: # of words must be counted
my code:
Code:
int main()
{
char word;
FILE *in;
in=fopen("File.dat","r");
int i=0;
while(fscanf(in, "%c", &word)!=EOF)
{
printf("%c\n", word);
if(word=='\n'){
break;
printf("newline matched, stop reading file!\n");
}
i++;
}
printf("%d words are read, bye\n", i);
fclose(in);
return 0;
}
thanx for any help!!
-
%c is a single character, not a string.
%s is a string.
fgets() will read a string, including, but not past, the newline.
Quzah.