How to change the fd associated with the stdin file descriptor?

匆匆过客 提交于 2019-12-11 10:00:30

问题


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. 

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


回答1:


dup2(2) is probably the easiest way:

dup2(pfds[1], STDOUT_FILENO);


来源:https://stackoverflow.com/questions/18497401/how-to-change-the-fd-associated-with-the-stdin-file-descriptor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!