Thread: stdout,stdin,stderr querry

  1. #1
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41

    Question stdout,stdin,stderr querry

    Well i was going through the library files of standard input and output file to actually know the process followed for defining the output and input stream , i did got a definition of stdout and stdin but when i searched more i found out there structure too now my question is how actually in the background does this process happen like how is the stream sent to the screen or accepted from the keyboard , i am sending there definitions and structure please guide me.:
    Code:
    _IO_FILE *stdin = (FILE *) &_IO_2_1_stdin_;
    _IO_FILE *stdout = (FILE *) &_IO_2_1_stdout_;
    _IO_FILE *stderr = (FILE *) &_IO_2_1_stderr_;
    The structure of _IO_FILE
    Code:
    struct _IO_FILE {
      int _flags;        /* High-order word is _IO_MAGIC; rest is flags. */
    #define _IO_file_flags _flags
    
      /* The following pointers correspond to the C++ streambuf protocol. */
      /* Note:  Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
      char* _IO_read_ptr;    /* Current read pointer */
      char* _IO_read_end;    /* End of get area. */
      char* _IO_read_base;    /* Start of putback+get area. */
      char* _IO_write_base;    /* Start of put area. */
      char* _IO_write_ptr;    /* Current put pointer. */
      char* _IO_write_end;    /* End of put area. */
      char* _IO_buf_base;    /* Start of reserve area. */
      char* _IO_buf_end;    /* End of reserve area. */
      /* The following fields are used to support backing up and undo. */
      char *_IO_save_base; /* Pointer to start of non-current get area. */
      char *_IO_backup_base;  /* Pointer to first valid character of backup area */
      char *_IO_save_end; /* Pointer to end of non-current get area. */
    
      struct _IO_marker *_markers;
    
      struct _IO_FILE *_chain;
    
      int _fileno;
    #if 0
      int _blksize;
    #else
      int _flags2;
    #endif
      _IO_off_t _old_offset; /* This used to be _offset but it's too small.  */
    
    #define __HAVE_COLUMN /* temporary */
      /* 1+column number of pbase(); 0 is unknown. */
      unsigned short _cur_column;
      signed char _vtable_offset;
      char _shortbuf[1];
    
      /*  char* _save_gptr;  char* _save_egptr; */
    
      _IO_lock_t *_lock;
    #ifdef _IO_USE_OLD_IO_FILE
    };

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You need to keep digging through your specific implementation.

    FWIW, on a Unix/Linux system, writing to stdout will ultimately end up in a call to write with a file descriptor of 1.

    And for completeness,
    stdin is mapped to fd 0
    stderr is mapped to fd 2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You may not find the info you're looking for in the include files. You could write a short program using putchar() and trace it in a debugger. On windows you'll find it calling functions in msvcrt.dll.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    that i got that the file descriptor for output stream is 1 but how is this approached by a system , is it embedded in the os or can i also make an output stream pointer/descriptor.?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Amitesh93
    that i got that the file descriptor for output stream is 1 but how is this approached by a system , is it embedded in the os or can i also make an output stream pointer/descriptor.?
    Which OS do you have in mind? If you're asking with respect to Salem's example, then you could use the open function to obtain a file descriptor for the file identified by the given path.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    Quote Originally Posted by laserlight View Post
    Which OS do you have in mind? If you're asking with respect to Salem's example, then you could use the open function to obtain a file descriptor for the file identified by the given path.
    its not os specific question that i am asking lets say i give you a task to make a *stdout type of pointer which gives us an output stream to the scree how are you going to approach it lets say for linux?

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Amitesh93 View Post
    its not os specific question that i am asking lets say i give you a task to make a *stdout type of pointer which gives us an output stream to the scree how are you going to approach it lets say for linux?
    Your question is VERY operating system specific. The code you copied into your first post is implementation specific (i.e. specific to one compiler, and possibly to one operating system).

    Furthermore, your question is incoherent. You claim (incorrectly) your question it is not OS specific, and then ask for a solution for linux. Linux is an operating system.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Apr 2013
    Location
    Gurgaon, Haryana, India
    Posts
    41
    Quote Originally Posted by grumpy View Post
    Your question is VERY operating system specific. The code you copied into your first post is implementation specific (i.e. specific to one compiler, and possibly to one operating system).

    Furthermore, your question is incoherent. You claim (incorrectly) your question it is not OS specific, and then ask for a solution for linux. Linux is an operating system.
    i think i was not able to put my question in a proper way , what i asked above was the approach you will use to make a certain screen output stream pointer or any function which will allow us to direct the output to the screen , and for that i asked you to give me an example or guidance through a linux based OS.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > what i asked above was the approach you will use to make a certain screen output stream pointer or any function which will allow us to direct the output to the screen
    You either do
    Code:
    printf("This appears on stdout\n");
    fprintf(stdout,"This appears on stdout\n");
    Unless you're in the business of writing your own version of the C standard library, there isn't a lot of point in worrying that your particular version has
    _IO_FILE *stdin = (FILE *) &_IO_2_1_stdin_;


    As far as the interface to the standard library is concerned, every file access is via a FILE* handle.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programming in Mac. stderr and stdout redirection
    By sharonch in forum C Programming
    Replies: 0
    Last Post: 04-15-2013, 08:45 PM
  2. Redirecting stdout, stderr and stdin
    By iwabee in forum Linux Programming
    Replies: 9
    Last Post: 05-16-2005, 05:42 PM
  3. receiving stderr and stdout from popen
    By rotis23 in forum C Programming
    Replies: 1
    Last Post: 11-08-2002, 08:21 PM
  4. stderr vs stdout
    By JoshG in forum C Programming
    Replies: 6
    Last Post: 10-20-2002, 11:17 PM