简体   繁体   中英

Interactive scanf using pipe

I try to read a value from console in unix using pipe

exmpl.cpp:

#include <stdio.h>
int main()
{
 int d;
 scanf("%d",&d);
 printf("d=%d",d);
 return 0;
]

So, when i use

./a.out < tmp

I get the number from tmp immidiately, but i want to get number from console

Also, I tried to use

fscanf(stdin, "%d",&d); 

but it didn't help

You can't really do that. With < you've changed the stdin of the program, therefore you can't use stdin for user interaction anymore.

Consider using tmp file as an argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM