Skip to main content

Files and Streams

C++ File Handling is a crucial part of any serious C++ programming. This part of the tutorial will cover an important aspect of C++ file handling - Files and Streams. The tutorial is designed for beginners and takes a step-by-step approach to ensure that you understand every aspect of the lesson.

What is a File?

In C++, a file is a place on your physical disk where data is stored. Files are a way to store data for long periods of time - data stored in a file will still be there tomorrow, next week, or next year.

What is a Stream?

A stream is a logical entity that represents a medium to either input or output data. C++ uses a conveniently abstracted Stream I/O model to read and write to and from files. This model allows you to treat all forms of data sources in the same way - whether they are files, memory, or even network data.

There are three types of streams in C++:

  1. Istream: This is the input stream used to read data from files.
  2. Ostream: This is the output stream used to write data to files.
  3. IOstream: This is used for general input and output operations.

Opening a File

In C++, before you can read from or write to a file, you must first open it. Here is the basic syntax to open a file in C++:

#include <fstream> 
using namespace std;

int main () {
ofstream myfile;
myfile.open ("example.txt");
return 0;
}

In this code snippet, we first include the <fstream> library which contains the ofstream class that we use to create an object myfile. Then we call the open() function on myfile and pass in the name of the file we want to open.

Writing to a File

After opening a file, you can write data to it using the insertion (<<) operator.

#include <fstream>
using namespace std;

int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}

In this example, we open a file named example.txt and write Writing this to a file. to it. After writing, we close the file using the close() function.

Reading from a File

You can read the contents of a file using the extraction (>>) operator.

#include <fstream>
#include <iostream>
using namespace std;

int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}
return 0;
}

In this example, we open a file for reading using ifstream and then check if the file was successfully opened with myfile.is_open(). If the file is open, we read the file line by line using the getline() function and print each line.

Closing a File

After performing all necessary operations, it is very important to close the file. Closing a file will free up the resources that were tied with the file and it is done using the member function close().

myfile.close();

This is a basic introduction to file handling in C++. In the next sections, we will dive deeper into reading and writing files, file pointers and how to handle errors during file operations. Keep practicing and happy coding!