gpt4 book ai didi

c++ - QFile:避免覆盖现有文件的新数据

转载 作者:太空宇宙 更新时间:2023-11-04 16:12:56 26 4
gpt4 key购买 nike

是否可以做到以下几点?

    QFile file("Test.txt")
If (file.exists) {
//Start writing new data at the end of the file and DO NOT overwrite existing data
} else {
//Start from the beginning
}

最佳答案

试试这个。

QFile file("Test.txt")
if (file.exists()) {
if(file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
{
QTextStream out(&file);
out << "new data";
}
else
qDebug() << "file not open";
} else {
}

open() 返回 bool 值,所以不要忘记检查文件是否正确打开。

来自文档:

QIODevice::Append - 设备以追加模式打开,因此所有数据都写入文件末尾。

更多信息:http://qt-project.org/doc/qt-4.8/qiodevice.html#OpenModeFlag-enum

关于c++ - QFile:避免覆盖现有文件的新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26284642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com