我编译我的代码,然后运行:$ ./a.exe < input.txt 由于某些原因,它不会读取第一个字符(t从第一行中的测试中缺失) 并且还有一个奇怪的字符在最后。我如何对付这两个错误? 感谢重定向标准输入
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char c = getchar();
while (c != EOF){
printf("%c ",c);
c = getchar();
}
return (0);
}
样品执行:
$ ./a.exe < input.txt
e s t l i n e o n e
t e s t l i n e t w o
f i n a l l i n e ▒
2all回答人:请停止在您的代码段中使用“char c”。所有与char相关的函数都会返回整数,而不是字符,EOF也是如此。 int c - 正确,char c - 错误。 – user3125367 2014-10-01 02:52:30