Thread: Stdin stdout

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    11

    Stdin stdout

    Hi.
    If stdin and stdout are file streams shouldn't they be closed after their use?
    I know C++ treats IO streams like objects and therefore they can be closed automatically when their destructors are called (given the programmer closes them in the definition of the destructor).. but I am not sure if it's necessary to close them in the first place, because obviously a destructor call is not possible in C.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,616
    If stdin and stdout are file streams shouldn't they be closed after their use?
    These two files along with the stderr file are automatically closed when the program terminates. And no it is not necessary for you to close these files.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    As a general rule, whatever code is responsible for allocating resources (e.g. opening file/stream/socket, allocating memory, etc) should be responsible for freeing it as well. That helps avoid resource leaks. Thus, whatever code is responsible for opening the streams should be responsible for closing it. There is typically code that runs before your main function that opens stdin/stdout for you, and there is code that runs after main that closes them. This is usually put in place automatically by the compiler, so you don't have to worry about it.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    11
    All right. Thank you all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recreating stdout/stdin
    By Zarniwoop in forum C Programming
    Replies: 1
    Last Post: 10-02-2008, 06:22 PM
  2. Redirecting stdout, stderr and stdin
    By iwabee in forum Linux Programming
    Replies: 9
    Last Post: 05-16-2005, 05:42 PM
  3. stdin --> random interference --> stdout
    By j0hnb1ank in forum C Programming
    Replies: 3
    Last Post: 01-22-2003, 11:43 AM
  4. STDIN stdout
    By GreyMattr in forum Linux Programming
    Replies: 2
    Last Post: 08-01-2002, 01:29 PM
  5. stdin/stdout
    By lmmds2 in forum C Programming
    Replies: 2
    Last Post: 12-10-2001, 02:07 PM