如何在 c++ 中使用不同的 ifstream 模式?
- 根据参考资料,如果我使用 ifstream infile ( "test.txt" , ifstream::in ); 它将
允许在流上进行输入操作。
但是有哪些“输入操作”的例子是什么? - ifstream infile ( "test.txt" , ifstream::in | ifstream::binary ); 是使用多个标志的正确语法吗?
- 如果我将
ifstream::
更改为iso::
会有什么不同吗?
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从文件中读取,这意味着输入流可以支持的所有内容。请参阅 istream 成员函数。通常,您可以执行这两种格式化(使用
>>
)以及未格式化的读取(使用read
)。请记住,ifstream
是basic_ifstream
模板针对char
类型的特化。根据您的需要,例如要读取 UTF-16 编码文件,您可能必须使用不同的专业化 (wifstream
),甚至使用特殊的区域设置(读取 此 了解有关语言环境的更多信息)。是的。
不。
Reading from a file which would mean everything an input stream can support. See
istream
member functions. Typically, you can do both formatted (using>>
) and unformatted reads (usingread
). Remember thatifstream
is a specialization of thebasic_ifstream
template forchar
type. Depending on your needs, say to read UTF-16 encoded file, you may have to use a different specialization (wifstream
) or even use a special locale (read this to know more about locales).Yes.
No.
流操作是
提取<<
和插入>>
。当您执行以下假设时file
属于fstream
类型:您还可以将
stream
作为二进制流
处理。在这种情况下,它实际上看起来并不像数据的“流
”,但它使您可以随机访问数据。在某些情况下,您无法使用二进制模式,特别是当您的数据无法像网络流一样可用时。插入和提取是流上的两个主要操作。默认情况下,
ifstream
创建为输入流
。因此,在这种情况下,std::ios::in 是多余的。您正确使用了标志。所有流都继承自
ios
。因此,这些标志在两个地方都可用,您可以直接从 ios 或从 fstream 检索它们。Stream operations are
extraction <<
andinsertion >>
. When you do the following assumingfile
is offstream
type:You could also, deal with the
stream
as abinary stream
. In that case, it doesn't really look like a "stream
" of data but that gives you random access to the data. In some cases you can't use the binary mode, especially if your data is not available like a network stream. Insertion and Extraction, are the two main operations on streams.ifstream
is created as aninput stream
by default. So,std::ios::in
is redundant in this case. You are using the flags correctly.all streams inherit from
ios
. So, the flags are available in both places, you can either retrieve them fromios
directly or fromfstream
.