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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4023419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:29:04  来源:igfitidea点击:

C - Feof(stdin)-how to indicate end of input in Windows?

cwindowsstdineof

提问by Randalfien

    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");
}

Under linux ctrl+D indicates end of input , and for windows ctrl+z is supposed to do the trick, but it doesn't work. Any ideas?

在 linux 下 ctrl+D 表示输入结束,对于 windows ctrl+z 应该可以解决问题,但它不起作用。有任何想法吗?

回答by usta

Try pressing Enter after Ctrl+z

尝试在 Ctrl+z 后按 Enter

If still no luck, please try the C++ version:

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

and see if it does better?

看看它是否做得更好?

回答by Remy

#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();
}