Reading and Writing Files with Streams

Tutorial 2 of 5

Introduction

In this tutorial, you will learn how to use streams in C++ to read and write files. Specifically, we will cover the use of ifstream (input file stream) and ofstream (output file stream) classes provided by the C++ Standard Library.

By the end of this tutorial, you will be able to:

  • Understand the concept of streams in C++
  • Read data from a file using ifstream
  • Write data to a file using ofstream
  • Implement advanced file handling techniques

Prerequisites: Basic knowledge of C++ programming is required to fully understand the concepts explained in this tutorial.

Step-by-Step Guide

In C++, a stream is a sequence of bytes where we can write or read data. A file stream is just a stream associated with a file.

Reading from a File

Use the ifstream class to read data from a file. Here's how to do it:

  1. Create an object of the ifstream class.
  2. Use the open() function to open a file.
  3. Use the >> operator to read data from the file.
  4. Close the file using the close() function.

Writing to a File

Use the ofstream class to write data to a file. Here's how to do it:

  1. Create an object of the ofstream class.
  2. Use the open() function to open a file.
  3. Use the << operator to write data to the file.
  4. Close the file using the close() function.

Code Examples

Let's see some practical examples.

Reading from a File

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

int main () {
  ifstream myfile;   // Create an object of ifstream
  myfile.open ("example.txt");  // Open a file
  if (myfile.is_open()) {   // If the file is open, read from it
    string line;
    while (getline (myfile, line)) {
      cout << line << '\n';  // Output the text from the file
    }
    myfile.close();   // Close the file
  }
  else cout << "Unable to open file"; 
  return 0;
}

In the above example, we're reading line by line from the file "example.txt" and printing each line.

Writing to a File

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

int main () {
  ofstream myfile;  // Create an object of ofstream
  myfile.open ("example.txt");  // Open a file
  if (myfile.is_open()) {   // If the file is open, write to it
    myfile << "Hello World!\n";  // Write to the file
    myfile.close();   // Close the file
  }
  else cout << "Unable to open file";
  return 0;
}

In this example, we're writing the string "Hello World!" to the file "example.txt".

Summary

In this tutorial, you've learned how to use ifstream and ofstream in C++ to read from and write to files. You can now perform basic file operations using streams in C++.

Next, you might want to learn more about handling errors during file operations, or explore more advanced file handling techniques.

Practice Exercises

  1. Write a program that reads a text file and counts the number of lines in it.
  2. Write a program that copies the contents of one file to another.
  3. Write a program that writes the numbers 1 to 10 on separate lines in a text file.

Solutions and explanations will be provided in the next tutorial. To practice further, you may want to try modifying these programs or creating your own.