开发者

Redirect standard streams back to Console and Keyboard in C++

After redirecting standard input and output t开发者_如何学Co some files(for example) using freopen how can I redirect them back to where they were at the very beginning?


You can duplicate/clone the initial FD's using id = fcntl(blah,F_DUPFD,0), and then use dup2(0,id) to copy it back (after closing stdin!), then reopen the file using fdopen(). Repeat for the others. However, that probably doesn't get you what you want exactly - it gets you a random filehandle that is associated with FD 0, not changing 'stdin'.

Another kind-of ugly option is to spawn off a thread (which implicitly dups stdin/etc), fdreopen them, do your processing, then exit the thread (closing the reopened stdin/etc) and unblock the main process (which never was changed). This is indirect but probably both portable and guaranteed to work.


You can not, since underlying file descriptors was closed. But on windows you can try to open special file "CON", which corresponds to console.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜