How do I get the file pointer that is being referenced by a file descriptor?

不羁岁月 提交于 2020-03-04 18:47:39

问题


I am trying to use dup2 to communicate between parent and child in the following code.

#include <stdio.h> 
#include <unistd.h> 

void main(int argv, char *argc) { 
    int testpipe[2]; 
    pipe(testpipe); 
    int PID = fork(); 
    if (PID == 0) { 
        dup2(testpipe[0], 0); 
        close(testpipe[1]); 
        execl("./multby", "multby", "3", NULL); 
        close(testpipe[0]); 
        close(0); 
    } else { 
        dup2(testpipe[1], 1); 
        close(testpipe[0]); 
        printf("5"); 
        fclose(stdout); 
        close(testpipe[1]); 
        wait(NULL); 
    }  
}

I understand that fclose is needed to write the output into stdout. However, in the case I do not use stdout or stdin, (that is, I do not use file descriptors 1 and 0), is there anyway for me to get the file pointer being reference by a file descriptor so that I can get the file pointer as an argument for fclose?

来源:https://stackoverflow.com/questions/60254987/how-do-i-get-the-file-pointer-that-is-being-referenced-by-a-file-descriptor

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