简体   繁体   English

无法读取C中的EOF字符

[英]unable to read the EOF character in C

I have a problem with reading the EOF character for the last input in C 我在读取C中最后一个输入的EOF字符时遇到问题

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. 对于最后一个输入,没有换行符,对于一个非常小的字符串,j的值被设置为非常大的数字。 So, to consider the end of file, i modified the while condition to 因此,考虑文件的结尾,我将while条件修改为

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. 谢谢。

EOF是超出char范围的整数值(因为它的主要目的是指示不存在 char ),因此,如果您希望能够将值与EOF进行比较,则需要检索该值并将其存储为一个int而不是一个char

How did you declare name2 and set it? 您如何声明name2并进行设置的? char can't contain an EOF , but if you got it by the standard input functions, you should have a '\\0' terminates it. char不能包含EOF ,但是如果您是通过标准输入函数得到的,则应该以'\\0'终止它。 if so, just change the condition to 如果是这样,只需将条件更改为

while (name2[j] && name2[j]!='\n')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM