Andreas DM
Andreas DM

Reputation: 10998

Is there a difference between ifstream::binary and ios::binary?

I've seen code written like so:

ifstream fin;
fin.open("largefile.dat", ifstream::binary | ifstream::in);

Now this makes me confused, is there any difference at all between the above code and this code below using ios::binary and ios::in as replacement?

ifstream fin;
fin.open("largefile.dat", ios::binary | ios::in);

Upvotes: 4

Views: 500

Answers (2)

Ziezi
Ziezi

Reputation: 6467

From Josuttis N.M. - The Standard Library A Tutorial and Reference.

Chapter 15.2 Fundamental Stream Classes and Stream Objects

The stream classes of the IOStream library form a hierarchy:

enter image description here

The classes in this class hierarchy play the following roles:
1. The base class ios_base defines the properties of all stream classes independent of the character type and the corresponding character traits. Most of this class consists of components and functions for state and format flags.
2. The class template basic_ios<> is derived from ios_base and defines the common properties of all stream classes that depend on the character types and the corresponding character traits. These properties include the definition of the buffer used by the stream. The buffer is an object of a class derived from the template class basic_streambuf<> with the corresponding template instantiation. It performs the actual reading and/or writing.
3. The class templates basic_istream<> and basic_ostream<> derive virtually from basic_ios<> and define objects that can be used for reading or writing, respectively. Like basic_ios<>, these classes are templates that are parametrized with a character type and its traits. When internationalization does not matter, the corresponding instantiations for the character type char — istream and ostream — are used.
4. The class template basic_iostream<> derives from both basic_istream<> and basic_ostream<>. This class template defines objects that can be used for both reading and writing.
5. The class template basic_streambuf<> is the heart of the IOStream library. This class defines the interface to all representations that can be written to or read from by streams and is used by the other stream classes to perform the reading and writing of characters. For access to some external representation, classes are derived from basic_streambuf<>.

Upvotes: 2

David G
David G

Reputation: 96810

There's no difference. These names are inherited from the virtual base std::ios_base from which the concrete stream classes derive.

Upvotes: 7

Related Questions