File I/O

Programming

This program demonstrates file input/output operations using std::ofstream for writing and std::ifstream for reading.

Implementation

#include <iostream>
#include <fstream>

int main() {
    std::ofstream outfile("example.txt");
    outfile << "This is a test file." << std::endl;
    outfile.close();

    std::ifstream infile("example.txt");
    std::string line;
    std::getline(infile, line);
    std::cout << "Read from file: " << line << std::endl;
    infile.close();
    return 0;
}

Key Concepts

Output

Read from file: This is a test file.