Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个名为 DBFile 的文件。我正在使用以下代码:
QString DBfile ="C:/Users/E543925/Desktop/VikuTB.xml"; QFile newFile(DBfile); newFile.open( QIODevice::WriteOnly);
现在我想在文件中写一些东西,如果它是空的。Qt中如何检查文件是否为空?
在通过newFile.size()打开之前检查文件大小
添加附加标志并检查插入pointer:
pointer
newFile.open( QIODevice::WriteOnly|QIODevice::Append ); if (newFile.pos() == 0) { // is empty } else { // some data inside }
免责声明:未经测试的代码,现在我会花时间尝试它......
编辑:经过测试,似乎运作良好......