fstream.read进度指标

时间:2017-01-14 20:20:30

标签: c++ multithreading file progress

我尝试创建一个用于在C ++中读取文件的进度指示器。我面临的问题是

  1. tellg()tellp()在读取时只返回24或-1,即使文件大小约为400MB EDIT:从线程中读取文件tellg()返回正确的值
  2. 我不是使用循环读取文件,而是使用单个read - 指令
  3. 代码(简化):

    fstream file;
    
    void readFile(string filename)
    {
        file.open(filename, ios::in | ios::binary);
        file.read((char*) &buffer, buffersize);
    }
    void readFileWithProgressIndicator(string filename)
    {
        size_t filesize = getFileSize(filename)
        thread file_read(readFile, filename);
    
        for( ; ; )
        {
            cout << file.tellg() << ":" << file.tellp() << endl;
        }
        file_read.join();
    }
    

1 个答案:

答案 0 :(得分:0)

1-程序行为似乎是连贯的。看起来你刚刚用1个电话读取你的文件,我想你的缓冲区是24个。你能查一下吗?

2 - 提案

std::atomic<std::streampos> fg;
std::atomic<std::streampos> fp;

void readFile(string filename)
{
    fstream file;
    file.open(filename, ios::in | ios::binary);
    file.read((char*) &buffer, buffersize);
    fg=file.tellg();
    fp=file.tellp();
}
void readFileWithProgressIndicator(string filename)
{
    size_t filesize = getFileSize(filename)
    thread file_read(readFile, filename);

    for( ; ; )
    {
        cout << fg << ":" << fp << endl;
    }
    file_read.join();
}
相关问题