<fstream>
The <fstream>
header is part of the C++ Standard Library that provides classes for file input and output operations. It extends the functionality of <iostream>
to work with files, allowing programs to read from and write to files on the disk.
ifstream
(input file stream), ofstream
(output file stream), and fstream
(input/output file stream)istream
, ostream
, and iostream
respectively<iostream>
manipulators for formatting#include <fstream>
#include <iostream>
#include <string>
int main() {
std::ofstream outFile("example.txt");
if (outFile.is_open()) {
outFile << "Hello, File!" << std::endl;
outFile << "This is a test of file output." << std::endl;
outFile << "Numbers can be written too: " << 42 << std::endl;
outFile.close();
std::cout << "File written successfully." << std::endl;
} else {
std::cerr << "Unable to open file for writing." << std::endl;
}
return 0;
}
ofstream
object to write to a file named "example.txt"<<
operator to write strings and numbers to the file#include <fstream>
#include <iostream>
#include <string>
int main() {
std::ifstream inFile("example.txt");
std::string line;
if (inFile.is_open()) {
while (std::getline(inFile, line)) {
std::cout << line << std::endl;
}
inFile.close();
} else {
std::cerr << "Unable to open file for reading." << std::endl;
}
return 0;
}
ifstream
object to read from "example.txt"std::getline()
to read the file line by line#include <fstream>
#include <iostream>
#include <vector>
struct Person {
char name[50];
int age;
};
int main() {
// Writing binary data
std::vector<Person> people = {
{"Alice", 30},
{"Bob", 25},
{"Charlie", 35}
};
std::ofstream outFile("people.bin", std::ios::binary);
if (outFile.is_open()) {
for (const auto& person : people) {
outFile.write(reinterpret_cast<const char*>(&person), sizeof(Person));
}
outFile.close();
std::cout << "Binary data written successfully." << std::endl;
}
// Reading binary data
std::ifstream inFile("people.bin", std::ios::binary);
if (inFile.is_open()) {
Person person;
while (inFile.read(reinterpret_cast<char*>(&person), sizeof(Person))) {
std::cout << "Name: " << person.name << ", Age: " << person.age << std::endl;
}
inFile.close();
}
return 0;
}
fstream
std::ios::binary
flag to open files in binary modewrite()
and read()
methods for binary I/O operations<fstream>
header automatically includes <istream>
and <ostream>
seekg()
(for input) and seekp()
(for output)fail()
, bad()
, and eof()
methodsThe <fstream>
header in C++ provides powerful tools for file input and output operations. It defines three main classes: ifstream
for reading files, ofstream
for writing files, and fstream
for both reading and writing. These classes inherit from the corresponding iostream classes, allowing for seamless integration with existing I/O operations and manipulators.
The examples provided demonstrate key aspects of using <fstream>
:
1. Writing text data to a file, showcasing basic file output operations.
2. Reading text data from a file, illustrating file input and line-by-line processing.
3. Performing binary file I/O, demonstrating how to work with structured data in binary format.
File I/O is a crucial aspect of many C++ applications, enabling data persistence, configuration storage, and processing of large datasets. The <fstream>
library provides a flexible and efficient means to interact with files, supporting both text and binary modes.
Effective use of <fstream>
requires understanding file opening modes, error checking, and proper file management (opening and closing). It's also important to consider performance implications when working with large files or performing frequent I/O operations.
For C++ developers, mastering file I/O using <fstream>
is essential for creating robust applications that can interact with the file system, process data files, and implement persistent storage solutions. Whether dealing with simple text files or complex binary data structures, <fstream>
provides the necessary tools for efficient and reliable file operations.