Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fork, dup2, and redirection of the stdin/stdout

Status
Not open for further replies.

bobetko

Programmer
Jan 14, 2003
155
0
0
US
The code below is simply doing shell comand "ls" (as example).
Question is, how can I redirect output to go to the some file instead to stdout?

Code:
int filedesc(){
    int n, fd[2];
    pipe (fd);
    switch (fork()) {
        case -1:
             //error;
        case 0:
         close(fd[0]);
         dup2(fd[1], STDOUT_FILENO);
         close(fd[1]);
         execlp("ls", "ls", NULL);
   }
}

I tried to open file using "I/O low level open" (for writting) and to use its file descriptor to make redirection
like this:

Code:
int filedesc(){
    int n, fd[2];
    pipe (fd);
    switch (fork()) {
        case -1:
             //error;
        case 0:
         close(fd[0]);
         dup2(fd[1], fdout);  //my file descriptor
         close(fd[1]);
         execlp("ls", "ls", NULL);
   }
}

Didn't work.

Thanks....
 
If you're redirecting to a file, there is no pipe.
So assuming fdout is what you got from doing say
Code:
fdout = open("file.txt",O_WRONLY);

Then it would be
Code:
         dup2(fdout, STDOUT_FILENO);
         close(fdout);
         execlp("ls", "ls", NULL);

--
 
Thanks for your help again. I tried as you said and all I get is empty "file.txt". I guess execlp need to know somehow where to redirect its output? how?

Code:
int test(){
    fdout = open("file.txt",O_WRONLY | O_CREAT, 0600);
    switch (fork()) {
        case -1:
             //error;
        case 0:
          dup2(fdout, STDOUT_FILENO);
          close(fdout);
          execlp("ls", "ls", NULL);
   }
}

Thanks
 
*shrug*
No idea - works for me, I get a file with the expected contents.
Code:
$ cat bar.c && gcc bar.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int test(){
    int fdout = open("file.txt",O_WRONLY | O_CREAT, 0600);
    switch (fork()) {
      case -1:
             //error;
      case 0:
        dup2(fdout, STDOUT_FILENO);
        close(fdout);
        execlp("ls", "ls", NULL);
    }
}

int main (void) {
  test();
  return 0;
}

$ ./a.out && wc file.txt
 47  47 436 file.txt

Check the return result from open() to make sure you actually got a valid file descriptor.

--
 
You're right.
I mispelled file descriptor's name. (and that mispelled name was already declared as an integer)... It took me some time to figure it out.... thanks Salem
 
I came across an interesting issue related to this.

I have a C program which opens a log file, then dups the file descriptor to stderr (fd 2) and closes the original fd. I then can call fprintf(stderr,....); to write to the log file.

On Solaris and Red Hat, this works fine. However, when I attempted to port this code to Windows, it didn't work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top