stdinBackup = 4;
dup2(0, stdinBackup);
Currently I am doing the above to 'backup' stdin so that it can be restored from backup later after it has been redirected somewhere else. I have a feeling that I am doing a lot wrong? (eg arbitrarily assigning 4 is surely not right). Anyone point me in the right direction?
If you just want to make a general copy for your own use, there is no need to use dup2()
. Just use a plain dup()
:
#include <stdio.h>
int stdinBackup = dup(STDIN_FILENO);
You only need to use dup2()
when you care about the duplicate's actual value.
See here for the definition of the symbolic constant STDIN_FILENO
, which is far better than using a naked 0
in the code.
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.