定期分块写入文件

时间:2018-11-28 13:33:16

标签: qt qfile qbytearray

当前,我正在用数据填充数据后 将一个巨大的QByteArray写入文件:

QByteArray my_ba;

// Fill out my_ba in some loops

path = "/some/path.data";
QFile file(path);
file.open(QIODevice::WriteOnly);
file.write(my_ba);
file.close();

由于我的QByteArray的顺序为 GB ,为了减少内存使用量,我需要将我的QByteArray写到块中的文件 >。

Qt有方便的工具吗?有标准做法吗?

1 个答案:

答案 0 :(得分:0)

我最终定期像这样大块写入文件:

for(unsigned int j = 0; j < Count; ++j) {

    // Fill out QByteArray my_ba in every single iteration of the loop

    // ...

    // But write my_ba only periodically and then clear it!
    // period is picked in such a way that writing to file will be done for every 1MB of data
    if( j % period == 0){
            if(file){
                file->write(my_ba);
                file->flush();
                my_ba.clear();
            }
        }

    // ...
}

我在将QByteArray的内容写入文件后定期清除my_ba.clear(),因此我的QByteArray永远不会变大,因此其内存消耗减少。

相关问题