ChatGPT解决这个技术问题 Extra ChatGPT

如何在c ++中检查文件是否存在于Qt中

Qt中如何检查给定路径中是否存在文件?

我当前的代码如下:

QFile Fout("/Users/Hans/Desktop/result.txt");

if(!Fout.exists()) 
{       
  eh.handleError(8);
}  
else
{
  // ......
}

但是当我运行代码时,即使我在路径中提到的文件不存在,它也没有给出 handleError 中指定的错误消息。

我认为下面的@mozzbozz 可能有你的答案——别忘了接受/给分 :)

m
mozzbozz

(TL;底部的 DR)

我会使用 QFileInfo-class (docs) - 这正是它的用途:

QFileInfo 类提供与系统无关的文件信息。 QFileInfo 提供有关文件名和文件系统中的位置(路径)、其访问权限以及它是目录还是符号链接等信息。文件的大小和最后修改/读取时间也可用。 QFileInfo 也可用于获取有关 Qt 资源的信息。

这是检查文件是否存在的源代码:

#include <QFileInfo>

(别忘了添加相应的 #include 语句)

bool fileExists(QString path) {
    QFileInfo check_file(path);
    // check if file exists and if yes: Is it really a file and no directory?
    if (check_file.exists() && check_file.isFile()) {
        return true;
    } else {
        return false;
    }
}

还要考虑:您只想检查路径是否存在 (exists()) 还是要确保这是一个文件而不是目录 (isFile())?

小心exists() 函数的文档说:

如果文件存在则返回真;否则返回假。注意:如果 file 是指向不存在文件的符号链接,则返回 false。

这并不精确。它应该是:

如果路径(即文件或目录)存在则返回true;否则返回假。

TL;博士

(上面函数的版本更短,省了几行代码)

#include <QFileInfo>

bool fileExists(QString path) {
    QFileInfo check_file(path);
    // check if path exists and if yes: Is it really a file and no directory?
    return check_file.exists() && check_file.isFile();
}

TL;Qt >=5.2 的 DR

(使用 exists 作为 Qt 5.2 中引入的 static;文档说静态函数更快,但我不确定在使用 isFile() 方法时仍然是这种情况;在至少这是一个单线然后)

#include <QFileInfo>

// check if path exists and if yes: Is it a file and no directory?
bool fileExists = QFileInfo::exists(path) && QFileInfo(path).isFile();

只是一个建议,函数 bool fileExists(const QString &path) 中的代码可以进一步简化为:return checkFile.exists() && checkFile.isFile(); @mozzbozz
@Dreamer 感谢您的评论。你当然是对的,尽管这也是一个品味问题。我也添加了您的版本(我将在此处保留较长的版本,因为它可能对初学者更容易理解)。
@kayleeFrye_onDeck 请注意,您的编辑并不正确。文档说静态函数更快是正确的。但是,文档对于该功能的实际作用非常不精确。 exists 函数(静态与否)仅检查路径是否存在,而不检查是否存在文件。因此,如果存在具有给定路径的目录,那么您的建议也将返回 true! (刚刚在我的系统上用 Qt 5.10 测试过)
@kayleeFrye_onDeck 当然(在 Windows 下使用 Qt 5.6 测试):gist.github.com/mozzbozz/2e83d7e3452a07fa817980403c42eade -->是的,我认为这是一个误解。我的意思是如果给定的路径是目录,exists 函数(static 与否)将返回 true。但是问题是“我如何检查 file 是否存在”(而不是 directory)。看看链接的代码片段,我希望能解释我的意思。
但是等等,它变得更糟哈哈:i.imgur.com/5Hds4kA.png "file" sigh
D
Donald Duck

您可以使用 QFileInfo::exists() 方法:

#include <QFileInfo>
if(QFileInfo("C:\\exampleFile.txt").exists()){
    //The file exists
}
else{
    //The file doesn't exist
}

如果您希望它仅在 file 存在时返回 true,如果路径存在但为文件夹则返回 false,您可以将其与 QDir::exists() 组合:

#include <QFileInfo>
#include <QDir>
QString path = "C:\\exampleFile.txt";
if(QFileInfo(path).exists() && !QDir(path).exists()){
    //The file exists and is not a folder
}
else{
    //The file doesn't exist, either the path doesn't exist or is the path of a folder
}

注意:如果你给出一个目录的路径,你的源代码也将返回 true,即使它“只是”一个文件。 OP 要求检查文件,而不是路径。
@mozzbozz 如果您希望它在路径存在但为文件夹的情况下返回 false,则可以执行 QFileInfo(path).exists() && !QDir(path).exists()。我已经编辑了我的答案以添加它。
A
Anthony

您发布的代码是正确的。很可能还有其他问题。

试着把这个:

qDebug() << "Function is being called.";

在您的 handleError 函数内部。如果打印出上述消息,您就知道问题出在其他地方。


f
fredmaggiowski

这就是我检查数据库是否存在的方式:

#include <QtSql>
#include <QDebug>
#include <QSqlDatabase>
#include <QSqlError>
#include <QFileInfo>

QString db_path = "/home/serge/Projects/sqlite/users_admin.db";

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(db_path);

if (QFileInfo::exists(db_path))
{
    bool ok = db.open();
    if(ok)
    {
        qDebug() << "Connected to the Database !";
        db.close();
    }
}
else
{
    qDebug() << "Database doesn't exists !";
}

使用 SQLite 很难检查数据库是否存在,因为如果它不存在,它会自动创建一个新数据库。


这与问题有什么关系?
J
Jerry Coffin

我会跳过使用 Qt 中的任何内容,而只使用旧标准 access

if (0==access("/Users/Hans/Desktop/result.txt", 0))
    // it exists
else
    // it doesn't exist

@Styne666:我在 Windows 上知道的每个编译器都支持 access——当然是 MS 和 gcc 端口。 Intel 使用支持它的 MS 库,Comeau 使用后端编译器的库。
谢谢你让我做我的研究。我接受这似乎可行,但考虑到the comments on this answer,我仍然认为坚持使用 Qt 的选项(如果您有 Qt 项目)是更好的解决方案。
@Styne666:我完全不确定 Qt 是否提供任何东西来解决 setuid/setgid 程序的问题,这似乎是唯一重要的问题。他们争论了“跨平台”的含义以及应该关心哪些标准,但如果我们只关心 Qt 支持的平台,那这些大多是有争议的。