如何更改与stdin文件描述符关联的fd? [英] How to change the fd associated with the stdin file descriptor?

查看:89
本文介绍了如何更改与stdin文件描述符关联的fd?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将管道的写fd关联到stdout.

I want to associate pipe's write fd to the stdout.

int pfds[2];
char buf[30];

if (pipe(pfds) == -1) {
    perror("pipe");
    exit(1);
}

I want to associate pfd[1] to the stdout of the process. 

我了解,我们可以使用freopen将stdout重定向到文件.我希望得到类似的东西.

I understand, we can use freopen to redirect stdout to the file. I was hoping to get something similar to this.

推荐答案

dup2(2) 可能是最简单的方法:

dup2(2) is probably the easiest way:

dup2(pfds[1], STDOUT_FILENO);

这篇关于如何更改与stdin文件描述符关联的fd?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆