cfiledescriptordup2dup

Problem when redirecting standard output with dup and dup2 in c


I have a problem with dup and dup2.

int copy (char * file1, char * file2) {
        int dr = open(file1, O_RDONLY);
        if (dr == -1) {
                return -1;
        }

        int dw = open(file2, O_WRONLY|O_TRUNC|O_CREAT, 0644); 
        if (dw == -1) {
                return -1;
        }

        int save0 = dup(0); 
        int save1 = dup(1); 

        dup2(dr, 0); 
        close(dr); 

        dup2(dw, 1); 
        close(dw); 

        char c;
        while ((c = getchar()) != EOF) {  
                putchar(c); 
        }

        dup2(save0, 0); 
        dup2(save1, 1); 

        printf("Hi");
        return 0;
}

I don't understand why the data from my file file1 is displayed on the screen because dr is standard input and dw is standard output...

Thanks for the help.


Solution

  • You (probably) just need to flush the buffers:

    while ((c = getchar()) != EOF) {  
            putchar(c); 
    }
    fflush(stdout);   /* Actually write to the file */
    dup2(save0, 0); 
    dup2(save1, 1);
    

    Since you haven't included the full code, it's hard to be certain. Also, make sure you haven't done any read-like functions (eg fread, fgetc, etc) on stdin before this function is called.