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

发布于 2024-08-08 04:59:57 字数 299 浏览 1 评论 0 原文

  1. 根据参考资料,如果我使用 ifstream infile ( "test.txt" , ifstream::in ); 它将允许在流上进行输入操作。 但是有哪些“输入操作”的例子是什么?
  2. ifstream infile ( "test.txt" , ifstream::in | ifstream::binary ); 是使用多个标志的正确语法吗?
  3. 如果我将 ifstream:: 更改为 iso:: 会有什么不同吗?

谢谢

  1. According to the reference, if I use 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"?
  2. Is 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:: ?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮色兮凉城 2024-08-15 04:59:57
  1. 根据参考,如果我使用 ifstream infile ( "test.txt" ,
    ifstream::in );它将允许输入
    对流的操作。但什么是
    “输入”的一些示例
    操作”?

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

  • 是 ifstream infile ( "test.txt" , ifstream::in | ifstream::binary );
    使用多个的正确语法
    旗帜?
  • 是的。

  • 如果将 ifstream:: 更改为 iso:: 会有什么不同吗?
  • 不。

    1. According to the reference, if I use 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. Typically, you can do both formatted (using >>) and unformatted reads (using read). Remember that ifstream is a specialization of the basic_ifstream template for char 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).

    1. Is 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:: ?

    No.

    近箐 2024-08-15 04:59:57

    流操作是提取<<插入>>。当您执行以下假设时
    file 属于 fstream 类型:

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

    您还可以将 stream 作为二进制流处理。在这种情况下,它实际上看起来并不像数据的“”,但它使您可以随机访问数据。在某些情况下,您无法使用二进制模式,特别是当您的数据无法像网络流一样可用时。插入和提取是流上的两个主要操作。

    默认情况下,ifstream 创建为输入流。因此,在这种情况下,std::ios::in 是多余的。您正确使用了标志。

    所有流都继承自ios。因此,这些标志在两个地方都可用,您可以直接从 ios 或从 fstream 检索它们。

    Stream operations are extraction << and insertion >>. When you do the following assuming
    file is of fstream type:

    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. 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 an input 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 from ios directly or from fstream.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文