π 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 :
