unable to read the EOF character in C

北战南征 提交于 2019-12-24 15:36:30

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!