C语言:如何知道从文件重定向输入时何时没有来自stdin的更多输入

C Language: How to know when there is no more input from stdin when redirecting input from a file

我的程序应该按以下方式运行:

C 程序 < file.txt

file.txt 可以有任意多的数据行。例如,

2 3 G 5 6
5 6 7 
6 9 3 6 H
<<Blank line>>

有没有办法知道没有更多的输入行?每个文件末尾都有一个空行。

我能够读取行,但我的程序永远不会知道是否没有更多数据要读取,并且一直等待更多数据,就像它通常期望从标准输入中获得的那样。

我是这样读的

 while( fgets(line, sizeof(line), stdin) != NULL) {
   ... do something
}

EOF代表文件结束,所以你可以读取文件直到你点击EOF

int c = 0;
while ((c = getchar ()) != EOF) { ... }

编辑:

while ( fgets(line, sizeof(line), stdin) != NULL ) {
    ... do something
}

应该可以正常工作,因为 fgets() returns NULL 如果它到达文件末尾。至少在 Unix 中,像 OS (Unix/Linux/BSD/Mac OS) 一切都是文件,标准输入也是。所以你可以在标准输入上检查 EOF

当文件完成时,所有的输入函数都会给你一个文件结束指示。例如:

#include <stdio.h>

int main(void) {
    int count = 0;
    while (getchar() != EOF)
        ++count;
    printf("There were %d characters.\n", count);
    return 0;
}

将计算输入流中的字符数:

pax> ./testprog <testprog.c
There were 169 characters.

pax> echo -n hello | ./testprog
There were 5 characters.

如果您使用的是 fgets(从您的更新中可以清楚地看出),也可以轻松检测到:

#include <stdio.h>

static char buff[1000];

int main(void) {
    int count = 0;
    while (fgets(buff, sizeof(buff), stdin) != NULL)
        ++count;
    printf("There were %d lines.\n", count);
    return 0;
}

运行 将计算行数:

pax> ./testprog <testprog.c
There were 12 lines.

您可以在两种 情况下看到使用输入重定向或管道方法正确检测到文件结尾。如果您 运行 从终端读取代码,则只需使用您的环境提供的工具指示文件结束。

在类 UNIX 操作系统中通常是行首的 CTRL-D,或者行首的 CTRL-Z Windows 的行:

pax> ./testprog
this has
two lines
<Ctrl-D pressed (I run Linux)>
There were 2 lines.

由于您正在从 stdin 读取并使用 fgets() ,要终止循环或没有更多的行可以从用户那里获取,请检查 return 的值 fgets() 如下所示。

char *ptr = NULL;
while( (ptr= fgets(line, sizeof(line), stdin)) != NULL) {
    if( *ptr == '\n')
        break;
    else { /* do something */  }
}

最后当您按下 ENTER 键时 if 条件将为真并终止。