QDataStream和Flush 这是一个QC++和Linux中使用QDATASTRAM和QTemporaryFile的QT问题。p>
我在刷新QDataStream时遇到一些问题。QTextStream有一个flush函数,但是QDataStream显然不需要。(引用自2013年:)。我的问题是,这是否真的/仍然是这样,是否存在强制QDataStream刷新的方法 当我处理使用QDataStream写入的文件时,最后的写入次数丢失(每次写入5字节时为112字节,每次写入1字节时为22字节)。但是,如果我在文件末尾写入大量填充,那么所有内容都存在(除了最后几次写入填充)。这就是为什么我认为QDataStream没有被刷新到文件中 我正在处理的文件是中等大小的原始二进制文件(大约2MB) 下面是一个使用我处理文件的一些代码的简单示例:QDataStream和Flush 这是一个QC++和Linux中使用QDATASTRAM和QTemporaryFile的QT问题。p>,c++,qt,C++,Qt,我在刷新QDataStream时遇到一些问题。QTextStream有一个flush函数,但是QDataStream显然不需要。(引用自2013年:)。我的问题是,这是否真的/仍然是这样,是否存在强制QDataStream刷新的方法 当我处理使用QDataStream写入的文件时,最后的写入次数丢失(每次写入5字节时为112字节,每次写入1字节时为22字节)。但是,如果我在文件末尾写入大量填充,那么所有内容都存在(除了最后几次写入填充)。这就是为什么我认为QDataStream没有被刷新到文件中
void read_and_process_file(QString &filename) {
QFile inputFile(filename);
if (!inputFile.open(QIODevice::ReadOnly)) {
qDebug() << "Couldn't open: " << filename;
return;
}
QDataStream fstream(&inputFile);
QTemporaryFile *tempfile = new QTemporaryFile();
if (!tempfile->open()) {
qDebug() << "Couldn't open tempfile";
return;
}
QDataStream ostream(tempfile);
while (!fstream.atEnd()) {
int block_size = 5; //The number to read at a time
char lines[block_size];
//Read from the input file
int len = fstream.readRawData(lines,block_size);
QByteArray data(lines,len);
//Will process data here once copying works
//Write to the temporary file
ostream.writeRawData(data,data.size());
}
process_file(tempfile);
delete tempfile;
}
void read_和_process_文件(QString&filename){
QFile输入文件(文件名);
如果(!inputFile.open(QIODevice::ReadOnly)){
qDebug()此答案的第一部分与将文件刷新到磁盘的问题无关
使用!fstream.atEnd()
作为while
的条件不是一个好主意。请参阅。我将while
循环更改为:
const int block_size = 5; //The number to read at a time
char lines[block_size];
int len = 0;
while ( (len = fstream.readRawData(lines,block_size)) > 0) {
QByteArray data(lines, len);
//Will process data here once copying works
//Write to the temporary file
ostream.writeRawData(data,data.size());
}
但是,我不认为使用中间QByteArray
有什么意义。该循环可以简化为:
while ( (len = fstream.readRawData(lines,block_size)) > 0) {
//Write to the temporary file
ostream.writeRawData(lines, len);
}
如果您需要为其他事情处理QByteArray
,可以构造一个并使用它,但是调用ostream.writeRawData
不需要使用它
关于文件没有被刷新的问题,我建议使用嵌套的作用域来打开文件。文件应该在作用域的末尾被刷新并关闭
void read_and_process_file(QString &filename) {
QFile inputFile(filename);
if (!inputFile.open(QIODevice::ReadOnly)) {
qDebug() << "Couldn't open: " << filename;
return;
}
QDataStream fstream(&inputFile);
QTemporaryFile *tempfile = new QTemporaryFile();
if (!tempfile->open()) {
qDebug() << "Couldn't open tempfile";
return;
}
// Create a nested scope for the QDataStream
// object so it gets flushed and closed when the
// scope ends.
{
QDataStream ostream(tempfile);
const int block_size = 5; //The number to read at a time
char lines[block_size];
int len = 0;
while ( (len = fstream.readRawData(lines,block_size)) > 0) {
QByteArray data(lines, len);
//Will process data here once copying works
//Write to the temporary file
ostream.writeRawData(lines, len);
}
// The QDataStream should be flushed and
// closed at the end of this scope.
}
process_file(tempfile);
delete tempfile;
}
void read_和_process_文件(QString&filename){
QFile输入文件(文件名);
如果(!inputFile.open(QIODevice::ReadOnly)){
qDebug()在调用进程文件(tempfile)之前,尝试调用tempfile->close()
.Hank,这似乎解决了问题,谢谢。如果tempfile关闭,它不可能被清除吗?我将运行一些测试并检查它。不,QTemporaryFile::close
不会删除该文件。当QTemporaryFile
对象被销毁时,该文件会被删除。此外,我建议不要分配使用new
删除单个作用域中的对象,因为这可能会导致内存泄漏。在您的示例中,在无法打开tempfile
后会出现泄漏。只需在堆栈上分配您的对象:QTemporaryFile tempfile;
谢谢,好的建议。我将循环更改为仅在len时结束