无法读取C中的EOF字符
我在读取 C 中最后一个输入的 EOF 字符时遇到问题
j=0;
while(*(name2+j)!='\n'){
if(*(name2+j) == ' '){
j++;
continue;
}
d[tolower(*(name2+j))]++;
j++;
}
对于最后一个输入,没有换行符,对于一个非常小的字符串,j 的值被设置为非常大的数字。因此,为了考虑文件结尾,我将 while 条件修改为,
while(*(name2+j)!='\n' && (*(name2+j))!=EOF)
但仍然遇到同样的问题。有人可以告诉我这里是否遗漏了一些东西吗?谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
EOF
是一个char
范围之外的整数值(因为它的目的是表明没有char
存在),因此如果您希望能够将某个值与EOF
进行比较,那么您需要检索该值并将其存储为int
而不是>字符。
EOF
is an integer value outside the range of achar
(since its very purpose is to indicate that nochar
is present), so if you want to be able to compare a value toEOF
, then you need to retrieve and store that value as anint
rather than as achar
.你是如何声明
name2
并设置它的?char
不能包含EOF
,但如果您通过标准输入函数获得它,则应该有'\0'
终止它。如果是这样,只需将条件更改为How did you declare
name2
and set it?char
can't contain anEOF
, but if you got it by the standard input functions, you should have a'\0'
terminates it. if so, just change the condition to