相关疑难解决方法(0)

C++ cout和cin缓冲区,以及一般的缓冲区

有人可以更明确地解释缓冲区的概念吗?我知道缓冲区是存储字符的数据结构,以及从中读取数据的位置.冲洗缓冲区的想法是什么?

刷新缓冲区时,这是指写入存储在其中的字符的行为吗?

从文字:

To avoid the overhead of writing in response to each output request, the library uses the 
buffer to accumulate the characters to be written, and flushes the buffer, by writing its
contents to the output device, only when necessary. By doing so, it can combine several 
output operations into a single write.
Run Code Online (Sandbox Code Playgroud)

当提到"刷新"时,几乎使其听起来好像缓冲区正在写入但同时也被擦除.只是猜测.

那么,为了写入屏幕上的视图需要缓冲区刷新?

When our program writes its prompt to cout, that output goes into the buffer associated
with the standard output stream. Next, we attempt …
Run Code Online (Sandbox Code Playgroud)

c++ buffer flush

16
推荐指数
2
解决办法
8796
查看次数

如何在C++中阅读不断增长的文本文件?

我试图从正在增长的文件(类似于什么tail -F)中读取,但我的代码必定存在一些问题:

string   log, logFile("test.log");
size_t   p = 0;

while(true)
{
    ifstream ifs(logFile.c_str());

    ifs.seekg(p);  //*1

    while(ifs.eof() == false)
    {
        getline(ifs, log);

        cout << log << endl;

        p = ifs.tellg();  //*2
    }

    nanosleep(&pause, NULL);
}
Run Code Online (Sandbox Code Playgroud)

如果没有//*1和//*2的行,日志文件会被正确读取到最后,但是如果添加新行,则不会发生任何事情.

使用seekg和tellg我试图存储文件的当前结束位置,这样当我重新打开它时,我可以去那里并阅读已添加的内容.

我想知道我的代码有什么问题,如果真的有必要为此目的关闭并重新打开同一个文件.

谢谢.

c++ logging fstream stl seekg

10
推荐指数
2
解决办法
7083
查看次数

clang 3.3/Xcode&libc ++:std :: getline在调用ifstream :: clear()后不读取数据

以下程序演示了libc ++和libstdc ++之间std :: getline行为的不一致性(使用clang3.3).

程序打开文件testfile,读取它直到eof,然后使用ifstream :: clear清除错误位并再次尝试从同一个文件句柄读取以查看是否有新数据附加到文件中.

#include <fstream>
#include <iostream>
#include <unistd.h>

using namespace std;

int main() {
    ifstream* file = new ifstream("testfile");
    if ( ! file->is_open() ) {
        cout << "testfile does not exist" << endl;
        return -1;
    }

    while ( 1 ) {
        file->clear(); // remove end of file evil bits
        string line;

        // workaround:
        // file->seekg(file->tellg());

        while ( getline(*file, line) )
            cout << "read line: " << line << endl;

        if ( file->eof() )
            cout …
Run Code Online (Sandbox Code Playgroud)

c++ clang ifstream libstdc++ libc++

5
推荐指数
1
解决办法
510
查看次数

Linux中std :: cout的奇怪行为

我试图for使用2个嵌套循环打印结果std::cout.但是,结果不会立即打印到控制台,但会有延迟(在循环或程序完成后).

我不认为这种行为是正常的,在Windows打印下工作正常.该程序不使用线程.

哪里可能是问题?(Ubuntu 10.10 + NetBeans 6.9).

c++ printing cout

1
推荐指数
1
解决办法
2793
查看次数

标签 统计

c++ ×4

buffer ×1

clang ×1

cout ×1

flush ×1

fstream ×1

ifstream ×1

libc++ ×1

libstdc++ ×1

logging ×1

printing ×1

seekg ×1

stl ×1