Console streams
Absolutely everybody who uses C++ ought to know about streams. Everybody. They are an essential part of the C++ experience. Sadly, very few people really know much about them, even though nearly everybody has used them. Consider the following code:
int num = 17;
cout << "Hello world! My number is " << num << endl;
In a console app, it displays on the screen: "Hello world! My number is 17". Pretty much everybody has done something like that at some stage when learning C++. But did you know that "cout" is in fact an output stream which just happens to write to the console? Stream data can be diverted anywhere you like.
File streams
I've seen people struggle terribly with C-style "fopen(..)", "fread(..)", and "fwrite(..)" functions when doing file I/O. There is no need, because you can use file streams just like you used the console stream. Here's our console example adapted for file output:
int num = 17;
fstream file("test.txt", fstream::out);
file << "Hello world! My number is " << num << endl;
file.close();
That should give you a file named "test.txt" with the same text in it as we had on our console earlier.
String streams
Now for the really fun part. You can make a stream just store up the data you give it, then get the whole lot out as a std::string. No need for allocating char buffers and using C-style "sprintf" anymore. Here's how:
int num = 17;
stringstream stream;
stream << "Hello world! My number is " << num << endl;
string s = stream.str();
It's really that easy. You need to remember to call "stringstream::str()" to get your actual string object out, but that's all there is to it to write data of many kinds into a string. And you can convert data out of a string too, like this:
float f = 0.0f;
int i = 0;
stringstream("123.4") >> f;
stringstream("123.4") >> i;
At the end of that, your float will contain 123.4 and your int will contain 123. That kind of data extraction/conversion works for any stream, including the console and files.
Manipulators
The fun of C++ streams doesn't end there. There are wonderful things called manipulators which can modify your input/output. The "endl" which you can use to end a line (and flush the buffer) is an example. Here's a very handy one for programmers:
cout << hex << 29;
That will output "1d" to the console. The "hex" manipulator will modify the stream so that every number you sent down it will be expressed in hexadecimal. You can also use "oct" for octal, or "dec" to switch it back to decimal. There are also manipulators to express numbers in fixed-point or scientific notation ("fixed" and "scientific" respectively), as well as many more.
More information...
I heartily recommend you take a look at all that's on offer with C++ streams, because they can save you a lot of time and effort -- I wish somebody had told me about them when I was learning! I find CPlusPlus.com to be an invaluable reference on this and other topics, so here are some articles to get you started:
- iostream - general information about stream input/output
- fstream - for file input/output
- stringstream - for string reading/writing
- manipulators