C - 使用文件从 stdin 读取?

C - reading from stdin with a file?

因此,我在与 C 程序相同的目录中使用了一个文本文件,并且我使用 MinGW 作为编译器。这是我的输入:

./program "hello" > helloworld.txt

在我的主函数程序中,我有:

#include <stdio.h>
int main(int argc, char *argv[]) {
char c;
while ((c=getchar()) != EOF) {
printf("test\n");
}
return 0;
}

没有打印。文本文件中肯定有行。我不知道发生了什么。这个赋值使 fopen() 的使用无效。它只是 getchar() 和 stdin。

您的命令不是从文本文件读取,而是写入。如果你想从中读取,你需要这样做:

./program < helloworld.txt

helloworld.txt:

this
is a
test

输出:

test
test
test
test
test
test
test
test
test
test
test
test
test
test
test