Thread: Write to two files using only one file descriptor

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    6

    Write to two files using only one file descriptor

    The problem I am facing is the need to write to stdout and to another file. I want the output to be displayed on the screen and into the file. This program is similar to the script command already available, but it is very simplified.

    Anyway. So far I have found it simple to write to two file descriptors going to place (dup/dup2) but what I want to do is the opposite.

    So, like this:
    --<==
    not like this:
    ==>--.

    Make sense?

    I don't have any code yet, as I am solving the problem before I start to code.

    Thanks for your time!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try "tee":
    http://unixhelp.ed.ac.uk/CGI/man-cgi?tee

    Note that you CAN NOT write to different targets with one filedescriptror. One filedescriptor connects to exactly one fileobject [unless it's a closed filedescriptor, in whcih case it's not connected to any fileobject].

    The only real solution if you don't want to use tee is that you create a function like this:
    Code:
    write2(int fd1, int fd2, void *buf, size_t size)
    {
       write(fd1, buf, size);
       write(fd2, buf, size);
    }
    Obviously, you should check the result of the operation too - I simplified it to make it easy to read and understand.

    --
    Mats
    Last edited by matsp; 11-12-2007 at 09:34 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    6
    Thanks a lot, but that didn't really solve the issue...

    Basically, I am writing a simple shell. So the output from the commands entered into the shell (such as cp, rm, mv) would go to stdout and to the file. Also, stdin would be read into the program and into the file, though that would be easier.

    I was hoping to do this (simplified psuedocode):

    Code:
    if tracing
        set stdout to output to file and stdout //the hard part
        call executeCommand(command to be executed)  //does all the exec() work
    end if
    That being said, however, the assignment specifications do not specifically state that I have to output everything to the terminal.

    If I do not output to terminal, I think I could dup2(file, fileno(stdout)) and be done with it. What do you think of that?

    Again, thanks for your time!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is no way you can solve that, unless you use a pipe to read from the exec'd task, and then duplicate the write in your shell.

    Or you could read from the output file in your main application, and output that to the console. But that may not work very well...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    6
    I just figured it out...

    Code:
    #include <stdio.h>
    #include <fcntl.h>
    
    int main()
    {
       int file;
       int newstdout;
       file=open("filename", O_WRONLY);
    
       dup2(fileno(stdout), newstdout);
       perror("dup2);
       dup2(file, fileno(stdout));
       perror("dup2");
       printf("hello world\n");
       write(newstdout, "funny\n", 10);
       return 0;
    }
    I had to save the actual stdout somewhere else. I wont be able to get the output from commands into the actual stdout, but I can get my prompts to stdout.

    Anyway, thanks!

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    newstdout is not initialized.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  2. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM