STDIN,STOUT,STDERR

Hey guys,

So something I have been using for quite a while but I don't really know the nuances of what is really happening,

so STDIN or standard input is the input for a program and normally comes via keyboard, stdin is handled in c++ by the cin object which is a special instance of ostream.

so STDOUT or standard output is the output for a program and normally this is the console or terminal ( Shell ) , this is handled in c++ by cout a specialised instance of ifstream.

STDERR is similar to STDOUT but is used for errors.

so how does this all work? how does the operating system handle this, I mean does each program have their own STDIN,STDOUT and STDERR? or do all running programs share STD,STDOUT and STDERR among one another? also where is STDIN,STDOUT and STDERR located in memory, is it located in kernal space or part of RAM that is only accessible by the OS and I am guessing this is done using system calls?

and another important question, from what I have researched most people associate STDOUT,STDIN and STDERR with Linux or Unix like OS's, but none seem to mention windows, does Windows also use STDOUT,STDIN and STDERR?

thanks
Yes, Windows has stdout, stdin, and stderr in a similar fashion as *nix (I think Windows copied from unix).

Every program has separate output/input/error streams. You can combine them, e.g. on windows you combine stdout and stderr like this: https://stackoverflow.com/questions/1420965/redirect-windows-cmd-stdout-and-stderr-to-a-single-file

You can also redirect all the streams to input or output to files, as well as pipe the output from one program to the input from another program. This is helpful for chaining together simple programs to make a larger command.

I don't know the specifics of how they are implemented at the operating system/kernel level, so hopefully someone else can go into more details.
Last edited on
I have never split the streams in windows to multiple console windows. It is probably doable, but I don't know if getting there looks anything like it does on unix. ?
the cin object which is a special instance of ostream ... cout a specialised instance of ifstream.
You have it backwards. cin is an istream and cout is an ostream.

On UNIX systems, I/O is handled through file descriptors. These are small integers that identify an I/O mechanism. Just as a C++ istream can be a stringstream or an ifstream or any other sort of stream, a file descriptor can be associated with a terminal, a file, a pipe, a tape drive, a network connect, etc. The big difference is that file descriptors are all handled at the OS level. It's pretty clever, and it was all conceived nearly 50 years ago.

When a program starts file descriptor 0 is stdin, FD 1 is stdout, and FD2 is stderr. Exactly what those fd's point to depends on what they were set to by the program that launched your program (like the shell).

If it's a C++ program, the startup code associates fd 0, 1, and 2 with cin, cout and cerr also. Note that I said "startup code." That's the real entry point to the program: the code that the OS calls when it starts a program. In a C++ program, it's a bit of library code that creates cin/cout,cerr, calls the constructors on file-scope variables and does whatever other initialization is needed. When it's done, it calls main().
Thanks guys

If it's a C++ program, the startup code associates fd 0, 1, and 2 with cin, cout and cerr also. Note that I said "startup code." That's the real entry point to the program: the code that the OS calls when it starts a program. In a C++ program, it's a bit of library code that creates cin/cout,cerr, calls the constructors on file-scope variables and does whatever other initialization is needed. When it's done, it calls main().


oh nice, from what I've read and as you mentioned there are 3 streams stdout,stderr,stdin but in C++ not only do we have a way to deal with output to the console which is cout, and input which is normally from a keyboard cin, but we also have streams like fstream which allows us to send data to files and to the running program, that been said the question which I can derive from that is if STDOUT is cout and STDIN is cin, what is fstream or even networking sockets?

I'm going to take an educated guess here, please correct me if I'm wrong :) in Unix you can redirect output from the console to a file or to another program and you can also redirect input like wise to be from a file rather than keyboard using the < and > operators. so I'm guessing std::fstream and networking sockets do use STDOUT,STDIN,STDERR but the implementation uses redirection to accomplish this task? ( also would this be similar on windows ? )

You can write to any file you want by opening it with an fstream.With a little more difficulty, you can probably open a pipe or network connection and associate it with a C++ stream.

The only thing that sets cin, cout and cerr apart is that they are already opened when main() is called.
I am not sure about that last bit Adam.
C++ compiler down deep may use the same deep assembly routines as the OS's redirect, but I think the OS redirect is adding an extra unnecessary (to a program, internal to itself) layer that could be avoided? That is, I would think the OS has to do some extra work to tie 2 programs together that would not be necessary if it were all 1 program using sockets or whatever? I could be wrong...


Thanks guys :)
... in C++ not only do we have a way to deal with output to the console which is cout, and input which is normally from a keyboard cin, but we also have streams like fstream which allows us to send data to files and to the running program, ... , what is fstream or even networking sockets?


A couple things.

First, std::cout may not be the console. In embedded systems, there probably won't be a console, so std::cout may go to a UART port or possibly even a file. Same with std::cerr. In many desktop situations, std::cerr also gets routed to the console, but in some environments it might go to a file or a system dashboard or even a printer. So, just realize that the standard streams just mean that there are streams available for you to use, not that they are guaranteed to be a keyboard and console.

Second, it appears you are confusing classes and objects. The names std::cin, std::cout and std::cerr are objects of the std::istream and std::ostream classes. They are concrete and refer to specific streams in the system. The name std::fstream is a class. You must instantiate an object of type fstream (or ifstream or ofstream) in order to manipulate a file. The following are NOT similar statements:

1
2
std::cout << "Hello World!" << std::endl;
std::fstream << "Hello World!" << std::endl;  // error - std::fstream is not an object 


Instead, use something like:
1
2
std::ofstream my_ofstream("myfile.txt");
my_ofstream << "Hello World!" << std::endl;  // writes "Hello World!" to myfile.txt 
Second, it appears you are confusing classes and objects.

And STDIN, STDOUT, and STDERR are abstract terms not to be confused to the concrete instances cin, cout, and cerr in C++, and stdin, stdout, and stderr in C.

And realize that in C++ all of these streams are "different", the C versions may or may not be using the same streams as the corresponding C++ streams when "tied".

Also mixing C++ streams with C file streams can, in extreme circumstances cause data corruption.

Topic archived. No new replies allowed.