【问题标题】:C - Feof(stdin)-how to indicate end of input in Windows?C - Feof(stdin) - 如何在 Windows 中指示输入结束?
【发布时间】:2010-10-26 12:00:39
【问题描述】:
    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 表示 end of input ,而对于 windows ctrl+z 应该可以解决问题,但它不起作用。有什么想法吗?

【问题讨论】:

标签: c windows stdin eof


【解决方案1】:

尝试在Ctrl+z后按回车

如果还是不行,请尝试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";

看看效果好不好?

【讨论】:

  • 不,不起作用。它只是在 ctrl+z 后冻结,无法再输入,甚至无法输入。
  • 顺便说一句,您认为 scanf 不返回 EOF 吗?导致网络上的所有示例都可以使用 getchar ..但是由于我需要 integers ,所以我不想使用它..
  • @Randalfien:scanf 将在文件结束时返回 EOF。
  • @Randalfien:Ctrl+z 后跟 Enter 适用于我的代码、VC 10 Express 和 XP SP3。你用什么编译器?
  • 我使用 NetBeans,那是 gcc 编译器,我运行 Win7 .. 会不会是我的编译器配置有问题?
【解决方案2】:
#include<stdio.h>
#include<conio.h>
void main (void)
{
int x,y,m;
for(x=0;x>=0;x++){
    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");
}
getch();
}

【讨论】:

  • 请不要只为您的答案转储代码。也提供解释
猜你喜欢
  • 1970-01-01
  • 2016-08-01
  • 2013-11-30
  • 1970-01-01
  • 2019-03-08
  • 1970-01-01
  • 2011-05-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多