C++ fstream
In this page:
What is fstream?
std::fstream combines the capabilities of ifstream and ofstream into a single bidirectional stream object, letting you both read and write through the same handle without needing two separate stream objects for one file.
Example: What is fstream?
#include <iostream>
#include <fstream>
int main() {
std::fstream file("data.txt", std::ios::out);
file << "Hello";
file.close();
std::cout << "Written with a single bidirectional fstream" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reading and Writing Sequentially
A stream tracks its read/write position internally, so after writing to a file you can't immediately read back what you wrote without first moving the cursor -- call seekg() to reposition the read pointer (commonly back to the start with seekg(0)) before reading.
Example: Reading and Writing Sequentially
#include <iostream>
#include <fstream>
int main() {
std::fstream file("data.txt", std::ios::in | std::ios::out | std::ios::trunc);
file << "42";
file.seekg(0);
int value;
file >> value;
std::cout << value << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Appending with fstream
Opening an fstream with the ios::app flag appends new writes to the end of the file while still permitting reads elsewhere in the file through the same open stream -- useful for programs that need to both log new entries and inspect older ones without reopening the file.
Example: Appending with fstream
#include <iostream>
#include <fstream>
int main() {
std::fstream file("data.txt", std::ios::app | std::ios::in);
file << "Appended";
std::cout << "Appended while still allowing reads" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pointer Manipulation (seekg & seekp)
fstream actually maintains two separate position indicators: seekg() moves the "get" (read) cursor, and seekp() moves the "put" (write) cursor. You can pass an absolute byte offset or a relative offset from the beginning, current position, or end of the file to either.
Example: Pointer Manipulation (seekg & seekp)
#include <iostream>
#include <fstream>
int main() {
std::fstream file("data.txt", std::ios::in | std::ios::out | std::ios::trunc);
file << "0123456789";
file.seekg(3);
char c;
file.get(c);
std::cout << c << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Telling Positions (tellg & tellp)
tellg() and tellp() report the current byte offset of the read and write cursors respectively, without moving them. A common trick to measure a file's total size is to seekg to the end and then call tellg() to read off that final position.
Example: Telling Positions (tellg & tellp)
#include <iostream>
#include <fstream>
int main() {
std::fstream file("data.txt", std::ios::in | std::ios::out | std::ios::trunc);
file << "Hello";
file.seekg(0, std::ios::end);
std::cout << "Size: " << file.tellg() << 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: