Oleksa
Oleksa

Reputation: 665

Why specifying std::ios::in for ifstream?

I want to use ifstream to load a font file into the memory and give it to FreeType. I find the standard I/O facilities a little discouraging. For now my question is why I have to specify std::ios::in for ifstream? The name ifstream already implies input operations, so why do we need this needless confusing things? Can we specify ifstream with out? What will happens if I don't specify it? From an example on cppreference:

std::ifstream f1("test.bin", std::ios::binary);

Here the default argument std::ios::in is not used. Why?

Upvotes: 3

Views: 2550

Answers (1)

Jodocus
Jodocus

Reputation: 7581

For now my question is why I have to specify std::ios::in for ifstream?

You don't. It will always append the corresponding flag by the bitwise OR operation. The documentation explicitly states that:

First, performs the same steps as the default constructor, then associates the stream with a file by calling rdbuf()->open(filename, mode | std::ios_base::in)

Can we specify ifstream with out?

Yes. This would just request the file buffer to open the file in writing mode as well, possibly creating it in the process. Of course, the istream interface will not allow for any output operations, but you may re-use the filebuf for other operations/streams without closing/re-opening the file.

Upvotes: 4

Related Questions