What does closing file descriptor 1(STDOUT_FILENO) really signify - Unix

This is a discussion on What does closing file descriptor 1(STDOUT_FILENO) really signify - Unix ; What does closing file descriptor 1(STDOUT_FILENO) really signify? Does this mean that we cant anymore write on standard out.????...

+ Reply to Thread
Results 1 to 3 of 3

Thread: What does closing file descriptor 1(STDOUT_FILENO) really signify

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

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

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

  2. Re: What does closing file descriptor 1(STDOUT_FILENO) really signify

    On Mar 22, 12:21 pm, Sanchit 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

    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
    #include

    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

  3. Re: What does closing file descriptor 1(STDOUT_FILENO) really signify

    On Mar 22, 10:16 pm, "jason.cipri...@gmail.com"
    wrote:
    > On Mar 22, 12:21 pm, Sanchit 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
    >
    > 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
    > #include
    >
    > 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 !!

+ Reply to Thread