Linux环境下bool QFile::rename

bool QFile::rename(const QString &newName)

1、Renames the file currently specified by fileName() to newName. Returns true if successful; otherwise returns false.

重命名当前指定的文件名。如果成功返回 true ,其他返回 false。

2、If a file with the name newName already exists, rename() returns false (i.e., QFile will not overwrite it).

如果新命名的文件已经存在,函数将返回 false。(QFile 不会覆盖它)

3、The file is closed before it is renamed.

当需要重命名文件的时候,这个文件必须为关闭状态。

4、If the rename operation fails, Qt will attempt to copy this file's contents to newName, and then remove this file, keeping only newName. If that copy operation fails or this file can't be removed, the destination file newName is removed to restore the old state.

如果重命名操作失败,Qt 将会尝试拷贝改文件的内容至新文件,然后删除旧文件。如果拷贝操作失败或旧文件不能够被删除,将删除新文件以保持旧文件的状态。

例:

QString strAbsPath = "/home/test.txt";
QFile file("strAbsPath ");
file.rename("/home/test1.txt");

注意:

1、传入参数必须为目标路径+文件名,修改路径也应该是目标路径+文件名。若直接传入文件名,QFile将会根据环境变量寻找改文件,rename时也会将其储存至环境变量定义的路径。

2、bool QFile::rename(const QString &oldName, const QString &newName)     (静态重载函数)

上一篇:QT的UI风格设计


下一篇:Qt学习笔记--文件读写(QFile、QDataStream、QTextStream)