C++ ifstream & ofstream
In this page:
Writing with ofstream
std::ofstream is specialized for writing to files, and reuses the insertion operator (<<) you already know from std::cout -- once a file is open, file << "text" writes to it exactly the same way cout << "text" writes to the console.
Example: Writing with ofstream
#include <iostream>
#include <fstream>
int main() {
std::ofstream file("data.txt");
file << "Hello file";
file.close();
std::cout << "Written" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reading with ifstream
std::ifstream is specialized for reading from files, using the extraction operator (>>) just like std::cin. It reads whitespace-delimited tokens one at a time -- numbers, words -- converting them to the target variable's type automatically.
Example: Reading with ifstream
#include <iostream>
#include <fstream>
int main() {
std::ofstream out("data.txt");
out << "42";
out.close();
std::ifstream in("data.txt");
int value;
in >> value;
std::cout << value << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reading Line by Line
Because >> stops at whitespace, it can't read a line containing spaces in one call -- reading "Hello World" would stop at "Hello". std::getline(stream, str) reads an entire line, including internal spaces, up to the next newline, which is what you want for sentence- or record-based file formats.
Example: Reading Line by Line
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ofstream out("data.txt");
out << "Hello World";
out.close();
std::ifstream in("data.txt");
std::string line;
std::getline(in, line);
std::cout << line << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Appending with ofstream
Opening a file with plain ofstream truncates it, discarding any existing content before writing begins. Passing the ios::app flag instead positions every write at the end of the existing file, letting you add new data (like log entries) without destroying what was already saved.
Example: Appending with ofstream
#include <iostream>
#include <fstream>
int main() {
std::ofstream file("data.txt", std::ios::app);
file << "Appended text";
file.close();
std::cout << "Appended without truncating" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Safe File Writing Practices
Beyond just checking is_open(), inspect the stream's state after each read or write -- a failed write (for example, from a full disk) sets the fail bit even though the stream opened successfully, and code that doesn't check this can silently lose data.
Example: Safe File Writing Practices
#include <iostream>
#include <fstream>
int main() {
std::ofstream file("data.txt");
file << "Some data";
if (file.fail()) {
std::cout << "Write failed" << std::endl;
} else {
std::cout << "Write succeeded" << std::endl;
}
file.close();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: