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/