For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > March 2008 > What does closing file descriptor 1(STDOUT_FILENO) really signify









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author What does closing file descriptor 1(STDOUT_FILENO) really signify
Sanchit

2008-03-22, 7:13 pm

What does closing file descriptor 1(STDOUT_FILENO) really signify?

Does this mean that we cant anymore write on standard out.????
jason.cipriani@gmail.com

2008-03-22, 7:13 pm

On Mar 22, 12:21 pm, Sanchit <sanchitgupt...@gmail.com> wrote:
> What does closing file descriptor 1(STDOUT_FILENO) really signify?


It closes the stdout stream that was automatically opened for you when
the program started.

> Does this mean that we cant anymore write on standard out.????


Yes. The following program:

#include <stdio.h>

int main (int argc, char **argv) {
printf("output 1\n");
close(1);
printf("output 2\n");
return 0;
}

Will not print "output 2". The stdout stream has been closed and can
not be written to.

Once I saw a strange trick for redirecting stdout to other places,
using dup():

#include <stdio.h>
#include <fcntl.h>

int main (int argc, char **argv) {

int fd;

printf("output 1\n");

fd = open("outfile.txt", O_CREAT | O_WRONLY);
close(1);
dup(fd);

printf("output 2\n");

return 0;

}


The dup() function duplicates a stream handle, assigning it the lowest
available handle. Since 1 has been closed, dup() duplicates the output
handle to outfile.txt as 1. Subsequent writes to stdout go to that
file instead. I have seen strange things done with this along with
popen or fork+exec. Always thought it was kind of strange, but
interesting.

Jason
Sanchit

2008-03-22, 7:13 pm

On Mar 22, 10:16 pm, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.com> wrote:
> On Mar 22, 12:21 pm, Sanchit <sanchitgupt...@gmail.com> wrote:
>
>
> It closes the stdout stream that was automatically opened for you when
> the program started.
>
>
> Yes. The following program:
>
> #include <stdio.h>
>
> int main (int argc, char **argv) {
> printf("output 1\n");
> close(1);
> printf("output 2\n");
> return 0;
>
> }
>
> Will not print "output 2". The stdout stream has been closed and can
> not be written to.
>
> Once I saw a strange trick for redirecting stdout to other places,
> using dup():
>
> #include <stdio.h>
> #include <fcntl.h>
>
> int main (int argc, char **argv) {
>
> int fd;
>
> printf("output 1\n");
>
> fd = open("outfile.txt", O_CREAT | O_WRONLY);
> close(1);
> dup(fd);
>
> printf("output 2\n");
>
> return 0;
>
> }
>
> The dup() function duplicates a stream handle, assigning it the lowest
> available handle. Since 1 has been closed, dup() duplicates the output
> handle to outfile.txt as 1. Subsequent writes to stdout go to that
> file instead. I have seen strange things done with this along with
> popen or fork+exec. Always thought it was kind of strange, but
> interesting.
>
> Jason


Thanks alot Jason.... Ur Answer cleared all my doubts. And thanks for
sharing another example !!
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com