gpt4 book ai didi

c++ - fstream 和 ofstream 运算符 << 区别

转载 作者:太空狗 更新时间:2023-10-29 19:39:58 25 4
gpt4 key购买 nike

我想重载 operator<< 以将对象序列化到文件(追加)。我应该使用哪个流? ofstream 还是 fstream?有什么区别?

std::ofstream& operator<<(std::ofstream& ofs, const MyData&);
std::fstream& operator<<(std::fstream& fs, const MyData&)

谢谢 jack

最佳答案

您应该重载 ostream 的运算符,然后您可以自然地将它用于派生自该运算符的任何类的实例 - ofstream、fstream(继承自 iostream,它继承自 istream 和 ostream)、ostringstream 和 stringstream(继承iostream也是)

std::ostream& operator<<(std::ostream& os, const MyData&);

关于c++ - fstream 和 ofstream 运算符 << 区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3171588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com