3
  1. 根据参考资料,如果我使用ifstream infile ( "test.txt" , ifstream::in );Allow input operations on the stream.,那么“输入操作”的例子有哪些?
  2. ifstream infile ( "test.txt" , ifstream::in | ifstream::binary );使用多个标志的语法是否正确?
  3. 如果我更改ifstream::为会有所不同iso::吗?

谢谢

4

2 回答 2

1
  1. 根据参考资料,如果我使用 ifstream infile ( "test.txt" , ifstream::in ); 它将允许对流进行输入操作。但是有哪些“输入操作”的例子呢?

从文件中读取意味着输入流可以支持的所有内容。请参阅istream成员函数。通常,您可以进行格式化(使用>>)和未格式化读取(使用read)。请记住,这ifstream是类型basic_ifstream模板的特化char。根据您的需要,例如读取 UTF-16 编码文件,您可能必须使用不同的专业化 ( wifstream) 甚至使用特殊的语言环境(阅读内容以了解有关语言环境的更多信息)。

  1. 是 ifstream infile ( "test.txt" , ifstream::in | ifstream::binary ); 使用多个标志的正确语法?

是的。

  1. 如果我将 ifstream:: 更改为 iso:: 会有所不同吗?

不。

于 2009-10-23T03:11:12.290 回答
0

流操作是extraction <<insertion >>。当您执行以下假设 file时,fstream类型为:

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

您也可以streambinary stream. 在这种情况下,它看起来并不像“ stream”数据,但它可以让您随机访问数据。在某些情况下,您不能使用二进制模式,尤其是当您的数据不像网络流那样可用时。插入和提取是流上的两个主要操作。

ifstream默认情况下创建为input stream。所以,std::ios::in在这种情况下是多余的。您正确使用了标志。

所有流都继承自ios. 因此,标志在两个地方都可用,您可以ios直接从fstream.

于 2009-10-23T02:59:59.580 回答