Welcome to the fourth part of a ten-part series covering the use of streams with C++. Streams can save you a lot of time and effort that would otherwise be spent on trivial, tedious tasks. In this article, we'll show you how to use streams to improve the data flow of your applications.
Writing more advanced code in C/C++ might not always be a trivial task. Due to this, you might have to run an application several times before you manage to eliminate all of its problems. Now with most applications, some input is required at a basic level at least. The quantity of required input can turn out to be quite large, so typing inside the console each time is definitely a good way to waste your precious time.
However, programming in C++ should be about looking over the details and seeing the big picture instead of handling trivial tasks like typing in the same input repeatedly. The way to evade this kind of practice is to use input that will not be cleared after the console closes. Off course, we are talking about files, preferably in text format.
Throughout this article, you will learn how to achieve good data flow in both directions (in and out) with the help of streams. These are members of a greater collection of streams in the library section named Iostream.
I strongly recommend reading my introductory articles about streams that appeared here on the Developer Shed Network under the names Introduction to Streams and Iostream Library and Basic IO in C++ because these will provide you with a better understanding of streams if you are not yet familiar with them. However, it is your call if you want to read them or not. The articles in this series should be just as useful independent of each other.
If you do want to know more about these topics, however, I advise you to search for my article about streams in general called Introduction to Streams or, for a little more detail about the structure of the library, see the article titled Iostream Library and Basic IO in C++ (this also treats the cin/cout streams).
The header where all of this is included is named <fstream>, so be sure to include it each time you want to use the library. Adding the using namespace std will make your work easier, and the readability of your code will increase. To start using them, however, like anything in C++, you need to declare its type.