João Pires
João Pires

Reputation: 1007

std::ios::binary or std::ifstream::binary and similar?

While searching for file reading examples in C++, I notice that many examples use

Are there any differences in these examples, and if not, why are they both included in the STL?

Upvotes: 5

Views: 12665

Answers (2)

Emilio Garavaglia
Emilio Garavaglia

Reputation: 20730

They ARE the same, since ifstream inherit from ios...

There is no "idiom" there. Just opinions.

My personal one is that you shold protect you code in a way a change in the library it uses will retain you code consistency.

So your_file_stream.end is always coherent to your_file_stream whatever it is and however it is composed in present and future versions. And even if it happens to be different respect to other stream types, will always work.

If you don't have an instance, std::ifstream::binary is always coherent with ifstream, however it will be composed now and in the future. And will word even if it will be different from other types.

If you're writing a polymorphic runtime-function taking std::ios&, than std::ios::binary is fine.

However it will be unlikely those things will ever be changed, so ... it does not really matters.

Upvotes: 2

Bathsheba
Bathsheba

Reputation: 234695

std::ios::binary vs. std::ifstream::binary is largely down to personal choice, but I always use the more "fundamental" definition insofar that it's further up the inheritance tree if you get my meaning. Hence std::ios::binary is my choice.

As for std::ios::beg and your_file_stream.beg, I'd plump for the former: it's pointless and obfuscating to reach a static member via an instance.

Upvotes: 4

Related Questions