C - 从stdin读取文件?

时间:2015-09-17 00:25:08

标签: c file shell stdin getchar

所以我有一个文本文件,我在与我的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。

1 个答案:

答案 0 :(得分:3)

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

./program < helloworld.txt

helloworld.txt:

this
is a
test

输出:

test
test
test
test
test
test
test
test
test
test
test
test
test
test
test
相关问题