简体   繁体   English

创建备份文件描述符?

[英]create backup file descriptor?

  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). (例如,任意分配4肯定是不对的)。 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() . 如果您只想为自己的用途制作一般副本,则无需使用dup2() Just use a plain dup() : 只需使用普通的dup()

#include <stdio.h>

int stdinBackup = dup(STDIN_FILENO);

You only need to use dup2() when you care about the duplicate's actual value. 当您关心副本的实际值时,您只需要使用dup2()

See here for the definition of the symbolic constant STDIN_FILENO , which is far better than using a naked 0 in the code. 请参阅此处了解符号常量STDIN_FILENO的定义,这比在代码中使用裸0要好得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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