问题
I have a problem with reading the EOF character for the last input in C
j=0;
while(*(name2+j)!='\n'){
if(*(name2+j) == ' '){
j++;
continue;
}
d[tolower(*(name2+j))]++;
j++;
}
For the last input, there is no new line character, the value of j is getting set to very large number for a very small string. So, to consider the end of file, i modified the while condition to
while(*(name2+j)!='\n' && (*(name2+j))!=EOF)
but still i am having the same problem. Can someone tell if i am missing something here ? Thanks.
回答1:
EOF
is an integer value outside the range of a char
(since its very purpose is to indicate that no char
is present), so if you want to be able to compare a value to EOF
, then you need to retrieve and store that value as an int
rather than as a char
.
回答2:
How did you declare name2
and set it? char
can't contain an EOF
, but if you got it by the standard input functions, you should have a '\0'
terminates it. if so, just change the condition to
while (name2[j] && name2[j]!='\n')
来源:https://stackoverflow.com/questions/9253545/unable-to-read-the-eof-character-in-c