C++ File Handling Introduction
In this page:
Streams and Files
C++ handles file I/O through the fstream library, which builds on the same stream abstraction as cin and cout. Instead of reading from the keyboard or writing to the console, file streams read from and write to a file on disk, using nearly identical syntax to what you already know from console I/O.
Example: Streams and Files
#include <iostream>
#include <fstream>
int main() {
std::ofstream file("data.txt");
file << "Hello, file!";
file.close();
std::cout << "Written using the same << stream style as cout" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Opening a File
You can open a file either by calling the open() member function on an existing stream object, or by passing the file path straight into the stream's constructor -- the constructor form opens the file immediately, saving a separate call.
Example: Opening a File
#include <iostream>
#include <fstream>
int main() {
std::ofstream file1;
file1.open("data.txt");
std::ofstream file2("data.txt");
file1 << "Line 1";
file1.close();
file2.close();
std::cout << "Opened both ways" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Checking if File Open Succeeded
A file might fail to open for many reasons -- a typo in the path, missing permissions, or a nonexistent directory -- so always call is_open() (or check the stream in a boolean context) before attempting to read or write. Skipping this check leads to silent failures where every subsequent operation just does nothing.
Example: Checking if File Open Succeeded
#include <iostream>
#include <fstream>
int main() {
std::ofstream file("data.txt");
if (file.is_open()) {
file << "Success";
file.close();
}
std::cout << "Checked before writing" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Closing a File
C++ automatically closes an open file stream when its destructor runs, typically at the end of scope, which prevents most resource leaks even if you forget. Still, calling close() explicitly once you're done is good practice -- it flushes buffered writes to disk immediately rather than waiting for scope exit.
Example: Closing a File
#include <iostream>
#include <fstream>
int main() {
std::ofstream file("data.txt");
file << "Content";
file.close();
std::cout << "Closed explicitly" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
File Open Modes
Open mode flags like ios::out (write, overwriting existing content) and ios::app (append to the end without erasing what's already there) control what happens to existing file content. Choosing the wrong mode is a common bug -- accidentally using ios::out when you meant to append will silently wipe a file's contents.
Example: File Open Modes
#include <iostream>
#include <fstream>
int main() {
std::ofstream file("data.txt", std::ios::app);
file << "Appended line\n";
file.close();
std::cout << "Appended using ios::app" << std::endl;
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: