My Blog

My WordPress Blog

My Blog

My WordPress Blog

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 &lt;&lt; "Hello, World!\n";
outFile &lt;&lt; "This is a C++ file I/O example.\n";
outFile.close();
string line;
ifstream inFile("example.txt");
cout &lt;&lt; "Contents of the file:\n";
while (getline(inFile, line)) {
    cout &lt;&lt; line &lt;&lt; endl;
}
inFile.close();
return 0;
}
Basic File I/O

Leave a Reply

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

Scroll to top