0
0
C++programming~5 mins

Reading and writing files in C++

Choose your learning style9 modes available
Introduction

Reading and writing files lets your program save information and get it back later. This helps keep data even after the program stops.

Saving user settings so they stay the same next time you open the program.
Loading a list of names or scores from a file to use in your program.
Writing a report or log to a file to check what happened during the program.
Reading data from a file to process it, like numbers or text.
Storing game progress so players can continue later.
Syntax
C++
#include <fstream>

std::ifstream inputFile("filename.txt");  // to read from a file
std::ofstream outputFile("filename.txt"); // to write to a file

// Check if file opened successfully
if (inputFile.is_open()) {
    // read from file
}

if (outputFile.is_open()) {
    // write to file
}

// Close files when done
inputFile.close();
outputFile.close();

Use #include <fstream> to work with files.

ifstream is for reading files, ofstream is for writing files.

Examples
This writes the text "Hello, file!" into a file named example.txt.
C++
#include <fstream>
#include <iostream>

int main() {
    std::ofstream outFile("example.txt");
    if (outFile.is_open()) {
        outFile << "Hello, file!\n";
        outFile.close();
    }
    return 0;
}
This reads each line from example.txt and prints it to the screen.
C++
#include <fstream>
#include <iostream>
#include <string>

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

This program first writes two lines of text into a file called data.txt. Then it reads the file line by line and prints each line to the screen.

C++
#include <fstream>
#include <iostream>
#include <string>

int main() {
    // Write to file
    std::ofstream outFile("data.txt");
    if (outFile.is_open()) {
        outFile << "Line 1: Hello!\n";
        outFile << "Line 2: Writing to a file.\n";
        outFile.close();
    } else {
        std::cout << "Cannot open file for writing.\n";
        return 1;
    }

    // Read from file
    std::ifstream inFile("data.txt");
    std::string line;
    if (inFile.is_open()) {
        while (std::getline(inFile, line)) {
            std::cout << line << '\n';
        }
        inFile.close();
    } else {
        std::cout << "Cannot open file for reading.\n";
        return 1;
    }

    return 0;
}
OutputSuccess
Important Notes

Always check if the file opened successfully before reading or writing.

Remember to close files to save changes and free resources.

Use std::getline to read a whole line including spaces.

Summary

Files let programs save and load information outside the program.

Use ifstream to read files and ofstream to write files.

Always check if files open successfully and close them when done.