简体   繁体   English

如何在C ++中使用不同的ifstream模式?

[英]How to use different ifstream modes in c++?

  1. According to the reference, if I use ifstream infile ( "test.txt" , ifstream::in ); 根据参考,如果我使用ifstream infile ( "test.txt" , ifstream::in ); it will Allow input operations on the stream. 它将Allow input operations on the stream. But what are some of the examples of the "input operations"? 但是“输入操作”的一些示例是什么?
  2. Is ifstream infile ( "test.txt" , ifstream::in | ifstream::binary ); ifstream infile ( "test.txt" , ifstream::in | ifstream::binary ); the right syntax to use multiple flags? 使用多个标志的正确语法?
  3. Will it make a difference if I change ifstream:: to iso:: ? 如果将ifstream::更改为iso::会有所不同吗?

Thank you 谢谢

  1. According to the reference, if I use ifstream infile ( "test.txt" , ifstream::in ); 根据参考,如果我使用ifstream infile(“ test.txt”,ifstream :: in); it will Allow input operations on the stream. 它将允许在流上进行输入操作。 But what are some of the examples of the "input operations"? 但是“输入操作”的一些示例是什么?

Reading from a file which would mean everything an input stream can support. 从文件读取意味着输入流可以支持的所有内容。 See istream member functions. 请参阅istream成员函数。 Typically, you can do both formatted (using >> ) and unformatted reads (using read ). 通常,您可以同时执行格式化的读取(使用>> )和未格式化的读取(使用read )。 Remember that ifstream is a specialization of the basic_ifstream template for char type. 请记住, ifstream是一个专业化basic_ifstream模板char类型。 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). 根据您的需求,例如要读取UTF-16编码的文件,您可能必须使用不同的专业化( wifstream )甚至使用特殊的语言环境(阅读此内容以了解有关语言环境的更多信息)。

  1. Is ifstream infile ( "test.txt" , ifstream::in | ifstream::binary ); 是ifstream infile(“ test.txt”,ifstream :: in | ifstream :: binary); the right syntax to use multiple flags? 使用多个标志的正确语法?

Yes. 是。

  1. Will it make a difference if I change ifstream:: to iso:: ? 如果将ifstream ::更改为iso ::,会有所不同吗?

No. 没有。

Stream operations are extraction << and insertion >> . 流操作是extraction <<insertion >> When you do the following assuming file is of fstream type: 当您执行以下操作时,假设filefstream类型:

file << 5 << 6.5 << "Hello World!"; // insertion of data (output)
file >> x >> y >> str; // exaction of data (input)

You could also, deal with the stream as a binary stream . 您也可以将stream作为binary stream In that case, it doesn't really look like a " stream " of data but that gives you random access to the data. 在那种情况下,它看起来实际上不是数据的“ stream ”,而是使您可以随机访问数据。 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 an input stream by default. 默认情况下, ifstream被创建为input stream So, std::ios::in is redundant in this case. 因此,在这种情况下, std::ios::in是多余的。 You are using the flags correctly. 您正在正确使用这些标志。

all streams inherit from ios . 所有流都从ios继承。 So, the flags are available in both places, you can either retrieve them from ios directly or from fstream . 因此,标志在两个地方都可用,您可以直接从ios或从fstream检索它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM