Find the answer to your Linux question:
Results 1 to 8 of 8
Hi everybody! I have a little problem... I need to redirect stdout to a file and after that redirect the out again on the stdout. This is my script: int ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2006
    Posts
    6

    Redirect stdout to a file


    Hi everybody!
    I have a little problem... I need to redirect stdout to a file and after that redirect the out again on the stdout.
    This is my script:

    int pid;
    int fd;

    int main()
    {
    int defout = dup(1);
    fd=open("/prova/out.txt", O_RDWR|O_CREAT);
    dup2(fd, 1); // redirect output to the file
    pid = fork(); // create child
    if (!pid) // if pid==0 then its a child process
    {
    close(fd);
    close(defout);
    printf("STAMPA1\n");
    }
    dup2(defout, 1); // redirect output back to stdout
    close(fd);
    close(defout);
    waitpid(pid, 0, 0); // wait until child finishes
    printf("STAMPA2\n");
    }

    The problem is that it prints 2 times on the out.txt file
    STAMPA1
    STAMPA2

    and it prints 1 time on the stdout
    STAMPA2

    Why does it print 2 times on the out.txt file (I need to print only 1 time on the file)?
    What is wrong?

    Thanks for your help.

  2. #2
    Linux Enthusiast KenJackson's Avatar
    Join Date
    Jun 2006
    Location
    Maryland, USA
    Posts
    510
    I think I see the problem.
    Code:
    int pid;
    int fd;
    
    int main()
      {
      int defout = dup(1);
      fd=open("/prova/out.txt", O_RDWR|O_CREAT);
      dup2(fd, 1); // redirect output to the file
      pid = fork(); // create child
      if (!pid) // if pid==0 then its a child process
        {
        close(fd);
        close(defout);
        printf("STAMPA1\n");
         # <<< Missing an exit() here
        }
      # This part is executed twice, once by each process
      dup2(defout, 1); // redirect output back to stdout
      close(fd);
      close(defout);
      waitpid(pid, 0, 0); // wait until child finishes
      printf("STAMPA2\n");
    }

  3. #3
    Just Joined!
    Join Date
    Aug 2006
    Posts
    6
    Thanks for all, now it works.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Aug 2006
    Posts
    6
    How can I use only 1 process to print one time on a file and one time on stdout?
    For example, the first time that I receive a SIGUSR1 signal I print on a file and next times I receive the signal I print on stdout.
    I tried a similar script like that I wrote but it doesn't work.

    Thanks for your support.

  6. #5
    Linux Enthusiast KenJackson's Avatar
    Join Date
    Jun 2006
    Location
    Maryland, USA
    Posts
    510
    If you want to alternate between writing to stdout and a file, I would just keep a boolean variable to keep track of which I did last.
    Code:
    #include <stdio.h>
    main(int argc, char *argv[])
    {
        int last_to_file = 0;
        FILE *fd = fopen("/prova/out.txt", "a+");
    
        // ...
        fputs("STAMPA\n", last_to_file ? stdout : fd);
        last_to_file = !last_to_file;
        // ...
    }

  7. #6
    Just Joined!
    Join Date
    Aug 2006
    Posts
    6
    Why this code writes 2 times on the file?
    I would make something like this, but printing 1 time on the file STAMPA1 and 1 time on stdout STAMPA2.
    What is wrong?

    int main()
    {
    defout = dup(1);
    fd=open("/prova/out.txt", O_RDWR|O_CREAT);
    dup2(fd, 1); // redirect output to the file
    close(fd);
    close(defout);
    printf("STAMPA1\n");
    dup2(defout, 1); // redirect output back to stdout
    close(fd);
    close(defout);
    printf("STAMPA2\n");
    }

  8. #7
    Linux Enthusiast KenJackson's Avatar
    Join Date
    Jun 2006
    Location
    Maryland, USA
    Posts
    510
    I can't follow that logic. It seems very complex for a simple task.

    Also, I don't know why you are using low-level I/O, when stream I/O would surely work at least as well.

  9. #8
    Just Joined!
    Join Date
    Aug 2006
    Posts
    6
    My problem is on "dup"...
    I would like to print on a file at the beginning of the script and print back on the stdout at the end of the script...
    I would like to manage this "switch" with SIGNAL.
    When I send an USERSIGNAL the script has to print on the file and when I send the signal again it has to print on the stdout.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •