0
0
CppComparisonBeginner · 3 min read

Ifstream vs Fstream in C++: Key Differences and Usage

In C++, ifstream is used specifically for reading files (input), while fstream supports both reading and writing (input/output) to files. Use ifstream when you only need to read data, and fstream when you need to read and write in the same file.
⚖️

Quick Comparison

This table summarizes the main differences between ifstream and fstream in C++.

Featureifstreamfstream
PurposeRead-only file inputRead and write file input/output
Default Modestd::ios::instd::ios::in | std::ios::out
Can write to file?NoYes
Can read from file?YesYes
Use caseReading data from filesReading and modifying files
File stream typeInput file streamInput/output file stream
⚖️

Key Differences

ifstream is a specialized class derived from istream designed only for reading data from files. It opens files in input mode by default, so you cannot write to files using ifstream. This makes it simpler and safer when you only need to read data.

On the other hand, fstream inherits from both istream and ostream, allowing it to handle both input and output operations on files. It opens files in both input and output mode by default, so you can read from and write to the same file using a single object.

Choosing between them depends on your needs: if you only want to read data, ifstream is more straightforward. If you need to update or write new data to a file, fstream is the right choice.

⚖️

Code Comparison

Here is an example showing how to read a file using ifstream.

cpp
#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ifstream inputFile("example.txt");
    if (!inputFile) {
        std::cerr << "Failed to open file for reading." << std::endl;
        return 1;
    }

    std::string line;
    while (std::getline(inputFile, line)) {
        std::cout << line << std::endl;
    }

    inputFile.close();
    return 0;
}
Output
Hello, this is example text. This file is read using ifstream.
↔️

Fstream Equivalent

Here is how to read the same file using fstream. It can also write, but here we use it just for reading to show equivalence.

cpp
#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::fstream file("example.txt", std::ios::in);
    if (!file) {
        std::cerr << "Failed to open file." << std::endl;
        return 1;
    }

    std::string line;
    while (std::getline(file, line)) {
        std::cout << line << std::endl;
    }

    file.close();
    return 0;
}
Output
Hello, this is example text. This file is read using ifstream.
🎯

When to Use Which

Choose ifstream when your program only needs to read data from files. It is simpler and clearly expresses your intent to only read.

Choose fstream when you need to both read and write to the same file, such as updating contents or appending data. It provides flexibility for input/output operations.

Using the right stream type helps keep your code clear and prevents accidental file modifications.

Key Takeaways

ifstream is for reading files only, opening them in input mode.
fstream supports both reading and writing by opening files in input/output mode.
Use ifstream for simple read-only file operations to keep code clear.
Use fstream when you need to modify or write to files as well as read.
Choosing the correct stream type prevents accidental file changes and clarifies intent.