您可以使用QFile中的exists()函数检查文件是否存在,如果不存在则使用QFile的构造函数创建文件。下面是您可以使用的代码示例:
QDateTime time = QDateTime::currentDateTime(); QString str = time.toString()+“:”+mmsi+“,”+QString::number(lat,‘f’,2)+“,”+QString::number(lot,‘f’,2);
QFile file(“lat”); if(!file.exists()) {
file.open(QIODevice::WriteOnly | QIODevice::Text);
file.close();
} if(file.open(QIODevice::WriteOnly | QIODevice::Append)) {
file.write(str.toStdString().c_str());
file.close();
}
在这里,我们首先使用exists()函数检查文件是否存在。如果不存在,则使用open()函数创建文件。然后,我们打开文件以进行写入操作,并将数据追加到文件的末尾。最后,我们关闭文件。