Basic File I/O

A simple program to read and write text to a file.

cppCopy code#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    ofstream outFile("example.txt");
    outFile << "Hello, World!\n";
    outFile << "This is a C++ file I/O example.\n";
    outFile.close();

    string line;
    ifstream inFile("example.txt");
    cout << "Contents of the file:\n";
    while (getline(inFile, line)) {
        cout << line << endl;
    }
    inFile.close();

    return 0;
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *