代码之家  ›  专栏  ›  技术社区  ›  Megidd

定期分块写入文件

  •  0
  • Megidd  · 技术社区  · 7 年前

    目前我正在写一本巨大的 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 国标 s、 为了减少内存使用,我需要编写 QByteArray 归档 .

    Qt是否有任何方便的工具来执行此操作?有没有标准的做法?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Megidd    7 年前

    定期地 这样地:

    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 因此减少了费用。

    推荐文章
    PhilBot  ·  带fwrite的内存QFile
    11 年前