Streams

Can someone explain streams to me? Or link me to a guide?
do u mean the headers like iostream.h and iostream, or in-file streams? Inside a program, there's a few different streams. For example, cout << "whatever" is accessing a "stream"

A stream is basically where the data goes. It all flows through that stream (hence the name). When you use << operator, it is throwing all of the data afterwards (in this case, "whatever") into the stream. cout denotes which stream, in this case computer output. You can also have file streams, which sends the same data into a file, etc. There should be reference on this website somewhere
something like related to #include <fstream>
and os_open(" ")
such and such
thx
There are 3 basic types of streams in standard:
i/o stream
file stream
string stream

You interact with them in the exact same way.

For i/o streams you have ostream objects like cout and istream objects like cin. You interact with ostream objects like so cout << "Hello World";. This saves the string "Hello World" in the stream. If you do something like cout << 3.14159; it will save those digits as characters in the stream. You interact with istream objects like so: cin>> x; This will take whatever is in cin and stick it in x. If x is a char, it will save a char, if it is an int, it will take an int. Formatting is automatic.

Filestreams are often created like so: ofstream fout("output.txt"); ifstream fin("input.txt");. You would then use fout and fin the same way you would use cout and cin. The difference is that instead of being displayed on the console, it is saved or read from a file. You can also use a binary flag so that data can be saved as pure data instead of as characters. This saves space, helps with security, and lets you easily store large containers like structures then recall them easily without much unpacking. To use the binary feature open the file with: ofstream fout("output.txt",ios::out | ios::binary);

Stringstreams are just like iostreams except that they do not output to the console. You can add data to them and extract the data as required. One of the large advantages to this is that you can easily format numbers to strings and back to numbers. You would declare one of these as so: stringstream ss;. You can then write to the stringstream with the << operator and you can get from the stringstream with the >> operator.

For a tutorial:
http://cplusplus.com/doc/tutorial/files/
http://cplusplus.com/doc/tutorial/basic_io/

For references:
http://cplusplus.com/reference/iostream/
Last edited on
even better
Topic archived. No new replies allowed.