qt c语言 文件读取10GB大小,分2GB大小一个大小, 读取保存
时间: 2024-10-09 16:06:34 浏览: 35
DateView01.rar
在Qt中处理大文件,特别是10GB以上的文件,通常会采用分块读取和逐块保存的方式,以避免一次性加载整个文件到内存中。下面是一个基本的示例,展示了如何分2GB大小的部分读取一个文件,并保存成若干个小文件:
首先,创建一个类来管理这部分工作:
```cpp
#include <QObject>
#include <QFile>
#include <QByteArray>
class BigFileReader : public QObject
{
public:
explicit BigFileReader(const QString& filePath, const int blockSize)
: m_filePath(filePath), m_blockSize(blockSize), m_totalSize(0)
{
loadFileInfo();
}
void readAndSave()
{
QFile file(m_filePath, QIODevice::ReadOnly);
if (!file.open()) {
qCritical() << "Failed to open the file: " << file.errorString();
return;
}
while (!file.seekEnd(m_totalSize, Qt::SeekEnd)) {
QByteArray block;
file.read(&block[0], m_blockSize);
if (block.isEmpty())
break;
saveBlock(block);
m_totalSize += block.size();
}
file.close();
emit fileProcessingFinished();
}
signals:
void blockProcessed(qint64 blockSize);
void fileProcessingFinished();
private:
void saveBlock(const QByteArray& block)
{
quint64 partNumber = m_totalSize / m_blockSize;
QString outputFilePath = QDir::tempPath() + "/" + m_filePath.section('/', -1) + "_" + QByteArray::number(partNumber);
QFile outputFile(outputFilePath, QIODevice::WriteOnly);
outputFile.write(block);
outputFile.close();
emit blockProcessed(block.size());
}
void loadFileInfo()
{
QFile fileInfoFile(m_filePath + ".info", QIODevice::ReadOnly);
if (!fileInfoFile.exists()) {
qint64 fileSize = qRound(QFile::size(m_filePath) * 1.0 / 1e9); // Convert bytes to GB
emit fileProcessingFinished(); // If no .info exists, assume it's a large file
} else {
qint64 fileSize = fileInfoFile.readLine().toInt();
m_totalSize = fileSize * m_blockSize; // Total size in blocks
}
}
QString m_filePath;
int m_blockSize;
qint64 m_totalSize;
};
```
在这个例子中,我们假设文件已经有一个名为`.info`的辅助文件,用于记录原始文件的总大小,方便后续计算。如果没有,我们会直接假设这是一个大文件。
使用这个类的示例:
```cpp
BigFileReader reader("your_large_file.txt", 2 * 1024 * 1024 * 1024); // 2GB block size
QObject::connect(&reader, &BigFileReader::blockProcessed, [] (qint64 size) { /* Handle each block processed */ });
QObject::connect(&reader, &BigFileReader::fileProcessingFinished, [] () { qDebug() << "File processing finished"; });
reader.readAndSave();
```
阅读全文