C++ Tutorial · Chapter 12 of 13
C++ File Handling
Use fstream for simple file work.
ofstream writes files. ifstream reads files. Always close streams when done.
Classroom labs often rewrite a whole file to simulate an update.
File sample
Try it Yourself »
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream outFile("note.txt");
outFile << "Saved from C++ Tutorial" << endl;
outFile.close();
cout << "File saved." << endl;
return 0;
}
Classroom Tips
- Check whether the file opened successfully in real projects.