- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想做的是:
std::ofstream outFile (fname, std::ios::binary);
//...
outFile.flush();
outFile.close();
效果很好。但是当我尝试合并这两行时,因为 flush 返回了一个引用:
outFile.flush().close();
它给出错误提示:
error: ‘struct std::basic_ostream<char>’ has no member named ‘close’
然后我更仔细地查看了引用资料,发现它实际上返回 ostream
instread of ofstream
..
为什么会这样?是错误还是设计?
最佳答案
这是设计使然。
ostream
是一个包含将序列写入关联的流缓冲区的类,它是一个抽象类,负责写入和读取某物。某物可以是任何东西;文件、控制台、套接字等。
ofstream
实际上只是一个方便的类,它将 ostream
扩展为“自动”使用一种写入文件的流缓冲区,称为 filebuf
。 ofstream
提供的函数只有open
、is_open
和close
;这些实际上只是底层 filebuf
流缓冲区对象的文件特定功能的简单接口(interface)。
如果你真的想要,你可以创建一个 ostream
来做与 ofstream
完全相同的事情。它只是需要更多代码,而且不够漂亮或明确。
std::filebuf f;
f.open(fname, std::ios::binary);
std::ostream os(&f);
os.write("Hello world!", 12);
os.flush();
// Error! close() isn't part of the streambuf interface, it's specific to
// files.
os.close();
基本上,ofstream
实际上并没有做任何类型的写入,因此它没有实现像flush
和write
这样的功能。所有写入都是通过 ostream
类完成的,该类操作底层 streambuf
类,其工作是确保数据到达预期位置(即文件) .
关于c++ - 为什么 ofstream::flush() 返回 ostream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21951185/
我正在开发一个小型图书馆,我需要做的一件事是让访问者访问一些数据并返回结果。 在一些较旧的 C++ 代码中,访问者需要声明一个 typedef return_type .例如,boost::stati
我不明白这个 ostream 函数声明是什么意思: ostream& operator<< (ostream& (*pf)(ostream&)); (特别是 (*pf)(ostream&) 部分)。我
我正在寻找一种将内容从一个 ostream 复制到另一个的方法。我有以下代码: std::ostringsteam oss; oss << "stack overflow"; { //do s
我有一个装满小雕像的游戏板。 分配: board = new Figure*[boardSize]; for (int i = 0; i > > board(boardSize, vector>(b
代码: cout SomeStream &operator SomeStream &operator (*this) << val; //Trouble in there! std::co
我正在对我的项目进行一些类型检查。下面的例子 using namespace std; cout ::value ? "TRUE":"FALSE" ) (); } 格式正确(source)。 std:
我到处搜索,但没有找到答案,所以如果这是重复的,请原谅。 我有一些非常古老的 C++ 代码,我正试图将它们轻松地转换为这个千年。代码仍然在 Visual Studio 6 中编译并且需要继续这样做,但
在我的 C++ 代码中,我不断地将不同的值写入文件。我的问题是,考虑到文件已成功打开这一事实,如果在任何情况下 write 或 << 会失败。我是否需要检查每次调用 write 或 << 以确保它已正
这个问题在这里已经有了答案: Calling a function overloaded in several namespaces from inside one namespace (3 个答案
我只是在写一些代码来吐出一个 wave header。我开始输入: file > 运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com
我有一个派生自 std::ostream 的类(用于日志记录)。剥离下来,它看起来像这样: class bsgs : public std::ostream { public: bsgs(cons
我有以下代码: class A { public: ... C *func() { ... } void func2() { ... } ... }; class B
我在一个抽象类的头文件中找到了这个函数: virtual ostream & print( ostream & out ) const; 谁能告诉我这是什么函数以及如何在派生类中声明它?据我所知,它似
我想写一个函数输出一些东西到 ostream传入并返回流,如下所示: std::ostream& MyPrint(int val, std::ostream* out) { *out << val
偶尔我会写一个类(T 说)并尝试覆盖 std::ostream& operator<<(std::ostream&, const T&) 但它不适用于某些类。这是一个(简化的)类的示例,它对我不起作用
我一直在尝试创建一个程序来为一组已定义的进程实现实时调度算法。使用 g++ 编译时出现错误,其中指出: RTSprocess.h:在函数“std::ostream& operator #include
因此,我得到了一个带有起始代码的任务来实现一个链表(我已经成功地完成了一个未排序的双向链表)并且在给定头文件的起始代码中有一个友元声明似乎有允许我使用 cout 打印链表的目标陈述。这是头文件;请注意
我正在努力 cout using namespace std; ostream& Print(ostream& out) { out using namespace std; ostream&
为了序列化任何对象(即对于没有全局 ostream& operator std::ostream& operator std::string serialize_any(const T& val) {
我宁愿实现一个non-friend函数,直接将函数标记为virtual。 但我现在想确保一组特定的类实现重载 friend std::ostream& operator << (std::ostrea
我是一名优秀的程序员,十分优秀!