以输入从标准输入后freopen函数() [英] Taking input from stdin after freopen()
本文介绍了以输入从标准输入后freopen函数()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我想,我们怎么可以把输入从标准输入
我调用后再次:
Hi I want to how can we take input from stdin
again after I invoke:
freopen("Smefile.txt","r",stdin);
precisely我想我第一次在我的程序应该从一个指定的文件的下一部分将采取从标准输入输入的第一。
Precisely I want my first in first of my program should take input from a designated file the next part would take from the stdin.
我爱:
int a,b;
freopen("Smefile.txt","r",stdin);
scanf("%d",&a);
{
//some block here such that the next cin/scanf takes b from standard input
}
cin>> b;
cout <<a<<" "<<b;
任何想法?
推荐答案
您不能。使用
FILE *input = fopen("Smefile.txt","r");
// read stuff from file
fclose(input);
input = stdin;
代替。
当你在同一个流,我不会建议无论如何混合C ++和C风格的I / O这不会帮助。
This won't help when you're mixing C++ and C-style I/O on the same stream, which I wouldn't recommend anyway.
(如果你恰巧是在Linux上,您可以重新打开的/ dev /标准输入
来代替。在Linux / Unix上,你也可以用杰里棺材的 dup2
把戏,但甚至多毛。)
(If you happen to be on Linux, you can reopen /dev/stdin
instead. On Linux/Unix, you can also use Jerry Coffin's dup2
trick, but that's even hairier.)
这篇关于以输入从标准输入后freopen函数()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文