我把这段代码写成函数,每次调用这个函数,我都要写入文件.但是这个函数只写一次到文件.每当我从windows打开文件时我发现只写了一个字文件.每次打电话时如何连续写入文件?
/* Try and open a file for output */
QString outputFilename = "Results.txt";
QFile outputFile(outputFilename);
outputFile.open(QIODevice::WriteOnly);
/* Check it opened OK */
if(!outputFile.isOpen()){
qDebug() <<"- Error, unable to open" << outputFilename << "for output";
return ;
}
/* Point a QTextStream object at the file */
QTextStream outStream(&outputFile);
/* Write the line to the file */
outStream <<"\n"<< szTemp;//"Victory!\n";
/* Close the file */
outputFile.close();
Run Code Online (Sandbox Code Playgroud)
当该行outputFile.open(QIODevice::WriteOnly);
打开文件时,它将替换文件中已有的所有内容.尝试用以下代码替换该行:
outputFile.open(QIODevice::Append);
Run Code Online (Sandbox Code Playgroud)
以一种模式打开它,而不是将数据附加到文件中存在的任何内容.
请注意,为每一行打开文件是一种效率低下的解决方案,特别是如果要编写许多行.打开文件一次,然后在关闭之前将所有单词写入其中,将更快地工作.
归档时间: |
|
查看次数: |
997 次 |
最近记录: |