建议使用QSaveFile对文件系统进行原子写入。它怎麽工作?
QSaveFile::commit() fsync()文件到文件系统吗?
发布于 2022-10-13 06:46:59
是,也不是。
它写入临时文件fsync()-it,然后将其重命名为所需的文件名。但是重命名不是fsync()-ed。因此,在出现断电的情况下,新文件将在操作系统决定编写之前不会发生。
我的解决方案是在commit():(仅linux)之后调用它。
#include <unistd.h>
#include <fcntl.h>
saveFile.write(saveDoc.toJson());
saveFile.commit();
QFileInfo fi(saveFile.fileName());
int fd = ::open(fi.absolutePath().toLocal8Bit().data(), O_RDONLY);
::fsync(fd);
::close(fd);
https://stackoverflow.com/questions/74051505
复制相似问题