Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How this stdout redirection works?

Code below redirects stdout to a file fname & then redirects back to original stdout. It works fine for me. But I am not able to understand how it actually works. If anyone can help me understand I will appreiciate it.

    printf("\n This is console");
    fflush(stdout);
    fgetpos(stdout, &pos);
    fd = dup(fileno(stdout));
    freopen(fname, "a+", stdout);   

    printf("inside file op");  

    fflush(stdout);
    dup2(fd,fileno(stdout));
    close(fd);
    clearerr(stdout);
    fsetpos(stdout, &pos);
    printf("\nBack to Console");
like image 266
vindyz Avatar asked Aug 07 '11 19:08

vindyz


People also ask

How do I redirect stdout?

Redirecting stdout and stderr to a file: The I/O streams can be redirected by putting the n> operator in use, where n is the file descriptor number. For redirecting stdout, we use “1>” and for stderr, “2>” is added as an operator.

What is the meaning of 2 >& 1?

&1 is used to reference the value of the file descriptor 1 (stdout). Now to the point 2>&1 means “Redirect the stderr to the same place we are redirecting the stdout” Now you can do this.

How does redirection work in Linux?

Redirection is a feature in Linux such that when executing a command, you can change the standard input/output devices. The basic workflow of any Linux command is that it takes an input and give an output. The standard input (stdin) device is the keyboard. The standard output (stdout) device is the screen.

What is redirection explain with example?

Redirection can be defined as changing the way from where commands read input to where commands send output. You can redirect the input and output of a command. For redirection, metacharacters are used.


1 Answers

Let's go through it line by line. The first line prints something to stdout:

printf("\n This is console");

Then it flushes stdout so all the remaining data in the buffer gets sent to stdout and won't get mixed up with the file data:

fflush(stdout);

Now we store the current position of ourselves in stdout because otherwise if stdout was already directed to a file, we might (?) overwrite earlier parts of it.

fgetpos(stdout, &pos);

Now we clone the file descriptor of what's currently stdout. Since we're about to change where stdout points to, we need to keep a copy of the original:

fd = dup(fileno(stdout));

Now that we have everything preserved, we can reopen stdout as the file:

freopen(fname, "a+", stdout);

At this point, stdout has been redirected to the file. We can now print to it:

printf("inside file op");  

Now we're done printing to the file. We need to flush stdout (now the file) so it doesn't get mixed up with the normal stdout data:

fflush(stdout);

After that, we clone the original stdout file descriptor over the current stdout descriptor.

dup2(fd,fileno(stdout));

The cloned one can be closed now:

close(fd);

I'm not quite sure why this is here but this clears any errors that occurred writing to the file:

clearerr(stdout);

Now we restore our position in stdout. Again, as far as I know, this is only useful if it was originally redirected to a file:

fsetpos(stdout, &pos);

Now we're back to the original stdout, so we can print again:

printf("\nBack to Console");
like image 112
icktoofay Avatar answered Sep 22 '22 21:09

icktoofay