代码之家  ›  专栏  ›  技术社区  ›  Agrudge Amicus

fstream对象默认关联

  •  1
  • Agrudge Amicus  · 技术社区  · 5 年前

    在这个程序中:

    #include<iostream>
    #include<fstream>
    
    int main()
    {
        std::ofstream ob;
    
        ob<<"hello"<<std::endl;
        return 0;
    }
    

    这是哪里 hello 写入/输出自 ob

    2 回复  |  直到 5 年前
        1
  •  1
  •   Spencer    5 年前

    输出被忽略。

    当任何内容写入流时,它调用 overflow 方法(或内部等价物)的每个字符的关联streambuf。根据 cppreference.com for std::basic_filebuf :

    is_open() == false ),返回 Traits::eof() 在做任何事之前。

    一旦 溢出 返回另一个 eof ,它的 failbit

        2
  •  1
  •   Ron Alan    5 年前

    这个 std::ofstream 类模板有几个构造函数。本例使用 std::ofstream default constructor 构造一个对象 与文件关联。

    1) 默认构造函数:构造不关联的流 使用文件:默认构造 std::basic_filebuf 和构造 带有指向此默认构造的指针的基 std::basic_filebuf 成员。