File Handling

Files are essential for storing data persistently beyond the lifetime of a program. They allow you to create, read, write, and manipulate information on your computer's storage devices. C++ offers powerful mechanisms for file handling using file streams.

Basic Concepts

Basics

  • File: A collection of data stored on a storage device.
  • File Stream: A flow of data between a program and a file.
  • File Input Stream: Used for reading from a file.
  • File Output Stream: Used for writing to a file.

Overview

  • File Streams: C++ treats files as streams of bytes, similar to standard input (cin) and output (cout) streams. File streams provide a way to interact with files for reading or writing data.
 
  • File Modes: When opening a file, you specify a mode that determines the operations allowed:
    • ios::out (ofstream): Creates a new file or overwrites an existing one for writing.
    • ios::in (ifstream): Opens an existing file for reading.
    • ios::app (ofstream): Appends data to the end of an existing file.
    • ios::binary (fstream): Enables binary mode for reading or writing raw bytes. The default is text mode, which interprets line endings differently across operating systems.

File Handling Classes

C++ provides three primary classes for file handling, all declared in the <fstream> header:

  • ofstream (Output File Stream): Used to create new files or overwrite existing ones for writing data.
  • ifstream (Input File Stream): Used to open existing files for reading data.
  • fstream (File Stream): A more versatile class that can be used for both reading and writing to files.

File Handling Classes

C++ provides three primary classes for file handling, all declared in the <fstream> header:

  • ofstream (Output File Stream): Used to create new files or overwrite existing ones for writing data.
  • ifstream (Input File Stream): Used to open existing files for reading data.
  • fstream (File Stream): A more versatile class that can be used for both reading and writing to files.

File Output in C++

Opening a File for Output

Similarly, we use the ofstream class to perform output operations on a file.

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

int main() {
    ofstream outputFile;
    outputFile.open("output.txt");

    if (!outputFile) {
        cout << "Unable to open file";
        return 1;
    }

    // File is opened successfully, perform operations...

    outputFile.close();

    return 0;
}

				
			

Writing to a File

Once the output file is open, we can write data to it using the insertion operator <<.

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

int main() {
    ofstream outputFile;
    outputFile.open("output.txt");

    if (!outputFile) {
        cout << "Unable to open file";
        return 1;
    }

    outputFile << "Hello, World!\n";
    outputFile << "This is a new line.";

    outputFile.close();

    return 0;
}

				
			
				
					// output // 
Output: (Contents of "output.txt")


				
			

File handling in C++ is a powerful feature that enables programmers to interact with external files efficiently. By understanding the basic concepts and utilizing the input and output stream classes (ifstream and ofstream), developers can manipulate files seamlessly. Whether it's reading from a data file or saving user-generated content, file handling provides a robust mechanism for data persistence in C++ programs.Happy coding !❤️

Table of Contents