8.3. Managing the Output Buffer8.3. 输出缓冲区的管理Each IO object manages a buffer, which is used to hold the data that the program reads and writes. When we write 每个 IO 对象管理一个缓冲区,用于存储程序读写的数据。如有下面语句: os << "please enter a value: "; the literal string is stored in the buffer associated with the stream os. There are several conditions that cause the buffer to be flushedthat is, writtento the actual output device or file: 系统将字符串字面值存储在与流 os 关联的缓冲区中。下面几种情况将导致缓冲区的内容被刷新,即写入到真实的输出设备或者文件:
Flushing the Output Buffer输出缓冲区的刷新Our programs have already used the endl manipulator, which writes a newline and flushes the buffer. There are two other similar manipulators. The first, flush, is used quite frequently. It flushes the stream but adds no characters to the output. The second, ends, is used much less often. It inserts a null character into the buffer and then flushes it: 我们的程序已经使用过 endl 操纵符,用于输出一个换行符并刷新缓冲区。除此之外,C++ 语言还提供了另外两个类似的操纵符。第一个经常使用的 flush,用于刷新流,但不在输出中添加任何字符。第二个则是比较少用的 ends,这个操纵符在缓冲区中插入空字符 null,然后后刷新它: cout << "hi!" << flush; // flushes the buffer; adds no data cout << "hi!" << ends; // inserts a null, then flushes the buffer cout << "hi!" << endl; // inserts a newline, then flushes the buffer The unitbuf Manipulatorunitbuf 操纵符If we want to flush every output, it is better to use the unitbuf manipulator. This manipulator flushes the stream after every write: 如果需要刷新所有输出,最好使用 unitbuf 操纵符。这个操纵符在每次执行完写操作后都刷新流: cout << unitbuf << "first" << " second" << nounitbuf; is equivalent to writing 等价于: cout << "first" << flush << " second" << flush; The nounitbuf manipulator restores the stream to use normal, system-managed buffer flushing. nounitbuf 操纵符将流恢复为使用正常的、由系统管理的缓冲区刷新方式。 Tying Input and Output Streams Together将输入和输出绑在一起When an input stream is tied to an output stream, any attempt to read the input stream will first flush the buffer associated output stream. The library ties cout to cin, so the statement 当输入流与输出流绑在一起时,任何读输入流的尝试都将首先刷新其输出流关联的缓冲区。标准库将 cout 与 cin 绑在一起,因此语句: cin >> ival; causes the buffer associated with cout to be flushed. 导致 cout 关联的缓冲区被刷新。
The tie function can be called on either istream or an ostream. It takes a pointer to an ostream and ties the argument stream to the object on which tie was called. When a stream ties itself to an ostream, then any IO operation on the stream that called tie flushes the buffer associated with the argument it passed to tie. tie 函数可用 istream 或 ostream 对象调用,使用一个指向 ostream 对象的指针形参。调用 tie 函数时,将实参流绑在调用该函数的对象上。如果一个流调用 tie 函数将其本身绑在传递给 tie 的 ostream 实参对象上,则该流上的任何 IO 操作都会刷新实参所关联的缓冲区。 cin.tie(&cout); // illustration only: the library ties cin and cout for us ostream *old_tie = cin.tie(); cin.tie(0); // break tie to cout, cout no longer flushed when cin is read cin.tie(&cerr); // ties cin and cerr, not necessarily a good idea! // ... cin.tie(0); // break tie between cin and cerr cin.tie(old_tie); // restablish normal tie between cin and cout An ostream object can be tied to only one istream object at a time. To break an existing tie, we pass in an argument of 0. 一个 ostream 对象每次只能与一个 istream 对象绑在一起。如果在调用 tie 函数时传递实参 0,则打破该流上已存在的捆绑。 ![]() |