C - Feof(stdin)-如何在 Windows 中指示输入结束?

标签 c windows stdin eof

    int x,y,m;
for(;;){
    m=scanf("%d %d",&x,&y);
    if (m!=2 || m==EOF){
        break;
    }
    else{
        printf("/%d/%d/\n",x,y);
    }
}
if (feof ( stdin )){
  printf("End of input\n");
}else if(m!=2){
  printf("There was an error\n");
}

在linux下ctrl+D表示输入结束,而在windows下ctrl+z应该可以做到这一点,但它不起作用。有什么想法吗?

最佳答案

尝试在 Ctrl+z 后按 Enter

如果还是不行,请尝试 C++ 版本:

#include <iostream>

int x, y;
while ( std::cin >> x >> y )
   std::cout << '/' << x << '/' << y << "/\n";
if ( std::cin.eof() )
   std::cout << "End of input\n";
else
   std::cout << "There was an error\n";

看看效果是否更好?

关于C - Feof(stdin)-如何在 Windows 中指示输入结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4023419/

相关文章:

c - setpwent 显示 valgrind 中的内存泄漏

hudson 的 CUnit 失败显示成功

linux - libiconv 在 windows/linux 上的问题

python - Python3中读取二进制文件stdin时出现UnicodeDecodeError

c - 为什么我得到一个close : Bad file descriptor error for this fork pipe c program?

r - 连接关闭的内存泄漏

计算字符串中的位数

c - Bitscan前向测试

java - tomcat启动但无法从另一台电脑上收听

windows - 仅注入(inject)函数并通过 CreateRemoteThread 运行它? C++