C++ Files and Streams

Computer programs are stored in the main memory during execution, and any data in the variables get wiped out after it is closed. To store data permanently, we need to write data to the secondary storage (hard disks, SSD, etc). C++ provides different ways to work with files and streams for reading and writing data.

Streams in C++

Streams are a sequential flow of data between an input and an output source. Input streams provide data to a program, while output streams receive/send out data from the program.

To work with files, we need to include the fstream header file.

File Streams

File streams allow us to send and receive data to/from files and programs. There are three types of file stream classes defined in the fstream header file:

  • ifstream – Input File Stream. Use to read any data from a file to a program.
  • ofstream – Output File Stream. Use to write data from a program to a file.
  • fstream – Can be used both as an input(ifstream) and an output stream(ofstream).

Always remember that:

  • To write some data to a file or create a new file, you must use an output stream(either ofstream or stream).
  • If you need to read data from an already existing file, then an input stream must be used(either ifstream or stream).

Types of files

Files are classified into two types based on the way they store data:

  1. Text Files – It contains data in the form of ASCII characters. It is used to store simple data like text, numbers, etc. It is slower than binary files.
  2. Binary Files – It stores data in a raw binary format and is not directly readable without a supporting program. Any type of data including C++ objects and structures can be stored in a binary file format.

Working with files in C++

To work with any file in C++, we must follow these steps:

  • First, define the type of stream you need (either an input stream or an output stream).
  • Depending on the type, define a new stream object using one of the stream classes(ifstream, ofstream, stream).
/* myfile can be used to only read files */
ifstream myfile;
/* myfile2 can be used to only write files */
ofstream myfile2;
/* myfile3 can be used to both read and files */
fstream myfile2;
  • Open the file you want to process. We can either use the open() method or the constructor function on the stream object.
/* open a file using an input stream object(myfile) for reading */
myfile.open("test.text");
/* or specify the filename when defining the object */
ifstream myfile("text.txt");
  • After opening the file, you can perform the operations that you want. There are various methods( eg: getline(), read(), write() ) and stream operators (<< and >>) to do that.
  • Finally, close the file stream object and free the memory. It is necessary to avoid any errors or other issues.
/* use the close() method of the stream object */
myfile.close();

Writing Data To a File in C++

We can write data to a file using any output stream object. We can write data to a file using the stream insertion operator (<<). It is very similar to cout that we learned in the previous lesson.

#include <fstream>

using namespace std;
int main() {
  int num = 5;
  /* define an output stream object and open a file */
  ofstream myfile;
  myfile.open("test.txt");
  /* write some data to text.txt */
  myfile << "Hello world\ n";
  myfile << "Num is" << num;
  /* close the file after usage */
  myfile.close();
}

The stream insertion operator (<<), outputs and inserts data into a file. We can output any valid data like strings, numbers, and variables to an existing or newly created file.

By running the code above, a new file “test.txt” will be created in the same directory, and write the content. If the file already exists, it will be overwritten and the previous contents will be lost.

Reading Data From a File in C++

To read data from a file, we need to use an ifstream object and stream extraction operator (>>). The stream extraction operator (>>) extracts the data from the file and stores it in a variable.

Whatever data we want to read, it must have matching data types with the variable to store.

Let’s say that we have a file called “test.txt” containing an integer and a character.

myfile.txt, 15 B

Then the program to read the contents of myfile.txt should be as below:

#include <fstream>

#include<iostream>

using namespace std;
int main() {
  int num;
  char ch;
  /* define an input stream object and open the file to read */
  ifstream myfile2;
  myfile2.open("myfile.txt");
  /* read the data from myfile.txt and store it */
  myfile >> num;
  myfile >> ch;
  /* cascasding is also allowed as myfile >> num >> ch; */

  /* output the read data to the console */
  cout << ”The num is: " << num << "and the character is" << ch;
  /* close the file after usage */
  myfile.close();
}

Reading Lines of text

Now, we can read simple data types like numbers, characters, etc. However, to read a file line by line, we need to use the getline() method.

So, the getline(string, length) method reads a line of text from the file and moves the read pointer forward to the next line.

myfile.txt

Hello world

How are you?

#include <fstream>
#include<iostream>

using namespace std;
int main() {
  char str[100];
  /* define an input stream object and open the file to read */
  ifstream myfile2;
  myfile2.open("myfile.txt");
  /* read and output the first line */
  myfile2.getline(str, 100);
  cout << str << endl;
  /* read and output the second line */
  myfile2.getline(str, 100);
  cout << str << endl;
  /*
  --Use loops when you have multiple lines to read--
  while(myfile2.getline(str, 100)){
  cout<<str<<endl;
  }
  */
  /* close the file after usage */
  myfile.close();
}

File Modes

The file stream’s open() method also takes a second argument which is the mode. File modes specify the behavior in which the file is opened.

file.open(filename, file mode);

Some of the important file modes are:

1. ios::in

Opens the file in reading mode. Defaults to true for ifstream objects.

2. ios::out

Opens the file in writing mode. Defaults to true for ofstream objects.

3. ios::app

Opens the file for appending data to it. The previous contents are preserved and any new data is added at the end of the file.

File Pointers and Random Access

Every file maintains two pointers – a get pointer and a put pointer. The get-pointer determines the position from where the file contents are read. Similarly, the put pointer determines the position where the contents will be written.

Every time we read the content of a file using a stream extraction operator (>>) or by using the getline() method, the get-pointer is moved forward automatically.

In the same way, after writing some data to a file, the put pointer is moved forward to the next empty position.

Controlling the get and put pointers

We can update the get and the put pointer positions manually using seekg() and seekp() method.

seekg(bytes to move)

seekg() moves the get pointer to the number of bytes specified from the beginning of the file.

Example:

/* will move the get pointer 30 bytes forward */
myfile.seekg(30);
/* now it will start reading after the first 30 bytes. */
myfile.getline(str, 100);

seekp(bytes to move);

seekp() works the same way as seekg() but it positions the put pointer which controls the position where the writing takes place.

Points to Remember

1. We need the fstream header file to work with files.
2. Use the ifstream (input file stream) class to read data from a file.
3. Use the ofstream (output file stream) class to write data to a file.
4. Use the stream insertion operator (<<) to write data to a file and the stream extraction operator (>>) to read data from a file to a variable.
5. We can also optionally specify file modes to control the behavior of a file when opening it.
6. Every file maintains a get pointer and a put pointer which determines the read and write position of a file in bytes.
7. We can randomly position the get and put pointers of a file using seekg() and seekp() functions.

Back to: Learn C++ Programming > Input/Output in C++

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.