【发布时间】:2019-07-10 02:55:35
【问题描述】:
由于某种原因,使用 c 重定向将输入传递到标准输入后,scanf 无法正常工作。 我已经测试了没有 c 重定向的相同代码,它工作得非常好。
另外,在使用 fflush(stdin) 进行 scanf 操作之前,我尝试过刷新标准输入,但也没有用。
像
一样运行我的可执行文件./3240Assignment0 < test-input.txt
运行scanf操作的代码如下
int main(int argc, char** argv){
fflush(stdin);
char input[100];
char *output = "Thank you for your message!";
puts("Tell me something nice:");
scanf("%s", input);
printf("%s\n\n", output);
}
问题是终端不给我机会进入 任何信息或输入。
我的终端脚本
Joes-MacBook-Pro:a0 joemanto$ make test
./3240Assignment0 < test-input.txt
Tell me something nice:
Thank you for your message!
Joes-MacBook-Pro:a0 joemanto$
【问题讨论】:
-
您为什么希望程序从终端读取?您已从文件中显式重定向标准输入,因此
scanf从文件中读取。