File I/O (fstream)
cin and cout. Reading and writing files works almost identically, thanks to the <fstream> header, which provides file-based stream classes that plug into the same << and >> operators you already know.The three stream types
Class | Purpose |
|---|---|
| Input file stream — for reading from a file. |
| Output file stream — for writing to a file. |
| Combined stream that can both read and write the same file. |
Writing to a file
write-file.cpp
#include <fstream>
#include <iostream>
int main() {
std::ofstream outFile("scores.txt");
if (!outFile) {
std::cerr << "Could not open scores.txt for writing.\n";
return 1;
}
outFile << "Alice 92\n";
outFile << "Bob 85\n";
outFile << "Carol 78\n";
// outFile closes automatically when it goes out of scope.
return 0;
}Checking that the file actually opened
false when it's in a failed state, so if (!file) is all you need.Reading a file line by line
std::getline(), which reads one line at a time into a std::string and returns the stream itself — which is falsy once there's nothing left to read, making it a natural loop condition.read-file.cpp
#include <fstream>
#include <iostream>
#include <string>
int main() {
std::ifstream inFile("scores.txt");
if (!inFile) {
std::cerr << "Could not open scores.txt for reading.\n";
return 1;
}
std::string line;
while (std::getline(inFile, line)) {
std::cout << "Read: " << line << "\n";
}
return 0;
}>>, just like with cin:read-formatted.cpp
#include <fstream>
#include <iostream>
#include <string>
int main() {
std::ifstream inFile("scores.txt");
std::string name;
int score;
while (inFile >> name >> score) {
std::cout << name << " scored " << score << "\n";
}
return 0;
}RAII closes the file for you
close() explicitly. File stream objects follow the same RAII pattern used throughout the standard library: the destructor closes the underlying file automatically when the stream object goes out of scope, whether the function returns normally or an exception unwinds the stack. You can call .close() manually if you need the file released before the end of scope, but it's rarely required.File modes
The second argument to a stream's constructor is a set of flags controlling how the file is opened. A few are especially common:
std::ios::app— append writes to the end of the file instead of overwriting it.std::ios::binary— open in binary mode, writing/reading raw bytes without any text translation.std::ios::trunc— truncate (clear) an existing file before writing (the default forofstream).Flags can be combined with the bitwise OR operator, e.g.
std::ios::out | std::ios::app.
append-mode.cpp
#include <fstream>
int main() {
std::ofstream logFile("app.log", std::ios::app);
logFile << "Application started\n";
return 0;
}