Learn about Input/Output & File Handling in C++ – Part-3



Input and Output data are not always coming from or going to the standard input (keyboard) and standard output (screen), respectively. Most of the time, data is read from a file, and output data is written to an output file. Add to this what we mentioned before that some widely-used operating systems like UNIX and Linux distributions consider everything as a file. This makes understanding how to deal with files a vital issue for a developer. This article will tackle this subject. So, are you ready? Let’s go!

File I/O
Fortunately, C++ has three classes for you that facilitate file input and output operations. The classes are:

  • ifstream for input.
  • ofstream for output.
  • fstream for both input and output from/to files.

The idea is simple: create an object of the appropriate class, for the file you want to use for input/output. Once the object is created, use the extraction operator >> with the input file stream object to extract (read) input(s) and assign it(them) to variable(s), this is for input. In output, the insertion operator << is used to send data to output file stream. When done with the required operation, the file is closed.
To understand it better, let’s illustrate the idea by examples.

Reading File Example
For a text file consisting of the following line:


We need to read the file, and print its contents. To do, consider the following program:

When executed, the program should print the following output:


Note that the stream object inputfile that we defined is used the same way we use cin. That shouldn’t be strange if you remember that cin itself is an object that represents the standard input stream. After extracting the input fields into the appropriate variables, and printing them, the file is closed using the close() member function.

This is for reading.
Now, let’s try the opposite.

Writing to File Example
The write operation is also straightforward, except one thing: if the named file exists, it is overwritten. If not, the file is created and has data written to it.

For this example we need to write the data of some football players to a file named players.txt. Copy the following code into your IDE, and execute it.

When executed, the program should create the players.txt file in the specified path, and write the data to it.


Similar to what we did with input, the insertion operator << was cascaded to write data to the file using the output stream object outfile.
Not only the extraction and insertion operators are useable by user-defined stream objects, but also the member functions like get(), getline() , read(), write() as well.

Append to File Example
Sometimes you need to keep the contents of the file you are writing to. For this case, the file could be opened in Appending mode. The following program will append two extra lines to the end of the players.txt file.

Where std::ofstream::app is the output mode.


Text Editor Example
Now, we are going to implement a very simple text editor. Our editor should prompt the user for the name of the output file. Then, it prompts the user to enter the text to save. When the stop character (delimiter) is encountered, the program stops reading new input, writes the entered text, and closes the created file.
Say hello to our first text editor!

Let’s give it a try!



In this article, we have discussed File I/O. Three main classes are used for input and output file streaming: ifstream, ofstream, and fstream.
Our next topic will be Command-Line arguments. An interesting topic to wait for. See you!


Please enter your comment!
Please enter your name here