Qt QFile::copy创建文件问题

版权声明: https://blog.csdn.net/luoshabugui/article/details/82141822

一 定义

看一下Qt assistant上关于QFile::copy的说明(Qt5.9.2):

bool QFile::copy(const QString &newName)
Copies the file currently specified by fileName() to a file called newName. Returns true if successful; otherwise returns false.
Note that if a file with the name newName already exists, copy() returns false (i.e. QFile will not overwrite it).
The source file is closed before it is copied.

QFile::copy会创建一个文件,如果newName标识的文件已存在,则不会重写它,结果通过返回值标明。

二 问题

最近发现在老项目上出现一个bug:一个ini的配置文件添加在qrc文件中,代码中通过QFile读取并copy到缓存目录中,配置文件内容在UI上显示并支持增删改,但在win10上,用户添加内容项后下次启动程序发现新添加内容丢失。

最开始一直怀疑是wfopen的问题,后来发现是只能r不能w, 打开配置文件属性是只读的。

也就是说 在win10上QFile::copy创建的文件属性默认是只读的,如果需要读写,可以在之后调用QFile::setPermissions函数设置文件的permissions和ownership

猜你喜欢

转载自blog.csdn.net/luoshabugui/article/details/82141822
今日推荐