ifstream::binary 和 ios::binary 有区别吗?
Is there a difference between ifstream::binary and ios::binary?
我见过这样写的代码:
ifstream fin;
fin.open("largefile.dat", ifstream::binary | ifstream::in);
现在这让我很困惑,上面的代码和下面使用 ios::binary
和 ios::in
作为替换的代码有什么区别吗?
ifstream fin;
fin.open("largefile.dat", ios::binary | ios::in);
没有区别。这些名称是从虚拟基础 std::ios_base
继承而来的,具体流 类 从中派生。
来自 Josuttis N.M。 - 标准库教程和参考.
第 15.2 章 基本流 类 和流对象
The stream classes of the IOStream library form a hierarchy:
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<>
.
我见过这样写的代码:
ifstream fin;
fin.open("largefile.dat", ifstream::binary | ifstream::in);
现在这让我很困惑,上面的代码和下面使用 ios::binary
和 ios::in
作为替换的代码有什么区别吗?
ifstream fin;
fin.open("largefile.dat", ios::binary | ios::in);
没有区别。这些名称是从虚拟基础 std::ios_base
继承而来的,具体流 类 从中派生。
来自 Josuttis N.M。 - 标准库教程和参考.
第 15.2 章 基本流 类 和流对象
The stream classes of the IOStream library form a hierarchy:
The classes in this class hierarchy play the following roles:
1. The base classios_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 templatebasic_ios<>
is derived fromios_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 classbasic_streambuf<>
with the corresponding template instantiation. It performs the actual reading and/or writing.
3. The class templatesbasic_istream<>
andbasic_ostream<>
derive virtually frombasic_ios<>
and define objects that can be used for reading or writing, respectively. Likebasic_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
andostream
— are used.
4. The class templatebasic_iostream<>
derives from bothbasic_istream<>
andbasic_ostream<>
. This class template defines objects that can be used for both reading and writing.
5. The class templatebasic_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 frombasic_streambuf<>
.