C++ File Streams β <fstream> for Reading and Writing Files
Introduction β Why File Streams Are Essential in C++
In C++, file streams provide an object-oriented way to read and write data to files using the same syntax as cin and cout. The <fstream> header defines specialized classesβifstream, ofstream, and fstreamβto handle file input/output operations easily and efficiently.
In this guide, youβll learn:
- What file stream classes are and how they work
- How to open, read, write, and close files
- Syntax and examples of reading/writing with streams
- File open modes and best practices
What Is <fstream>?
<fstream> is a C++ standard library header that includes the classes needed to handle file operations:
| Class | Purpose | Inherits From |
|---|---|---|
ifstream | Input stream for reading | istream |
ofstream | Output stream for writing | ostream |
fstream | Input/output stream | iostream |
Code Examples β Reading & Writing
Writing to a File Using ofstream
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream outFile("sample.txt");
outFile << "Hello, File Streams!" << endl;
outFile.close();
return 0;
}
Reading from a File Using ifstream
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream inFile("sample.txt");
string line;
while (getline(inFile, line)) {
cout << line << endl;
}
inFile.close();
return 0;
}
Using fstream for Both Reading and Writing
#include <fstream>
using namespace std;
int main() {
fstream file("data.txt", ios::in | ios::out | ios::trunc);
file << "Line 1\n";
file.seekg(0);
string line;
getline(file, line);
cout << line << endl;
file.close();
return 0;
}
File Opening Modes
File streams can be combined with mode flags:
| Mode | Description |
|---|---|
ios::in | Open for reading |
ios::out | Open for writing |
ios::app | Append to end of file |
ios::trunc | Truncate existing file (default for ofstream) |
ios::binary | Open file in binary mode |
File Handling Functions
| Function | Purpose |
|---|---|
.open() | Opens a file |
.close() | Closes the file |
.is_open() | Returns true if file successfully opened |
.eof() | Checks for end-of-file |
.fail() | Checks for failure in file operation |
Best Practices & Tips
Always check .is_open() before reading/writing
Use ofstream with ios::app to preserve existing data
Remember to .close() files to avoid resource leaks
Use relative paths to improve cross-platform portability
Summary β Recap & Next Steps
Key Takeaways:
- Use
ifstream,ofstream, andfstreamfrom<fstream>to handle files - Support text and binary data with different open modes
- Streams behave similarly to
cinandcout, making them easy to use
Real-World Relevance:
C++ file streams are used in logging systems, report generation, configuration loading, and all forms of persistent data handling.
Next Steps:
Learn how to read and write content using file streams with more advanced techniques and handle files in binary mode.
FAQ β C++ File Streams
Whatβs the difference between ofstream and fstream?ofstream is for writing only, while fstream supports both reading and writing.
Do I need to manually close the file?
Yes, always call .close() after you’re done to release resources.
What if I open a file that doesnβt exist?ifstream will fail to open it; ofstream will create it by default.
Can I check if a file opened successfully?
Yes. Use .is_open() to verify.
How do I avoid overwriting an existing file?
Use ios::app mode or check for file existence before writing.
Share Now :
