【问题标题】:How to check whether file exists in Qt in c++如何在c ++中检查文件是否存在于Qt中
【发布时间】:2012-05-03 16:01:09
【问题描述】:

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

我当前的代码如下:

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

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

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

【问题讨论】:

  • 我认为下面的@mozzbozz 可能有你的答案——不要忘记接受/给分:)

标签: c++ qt file-exists


【解决方案1】:

(TL;DR 在底部)

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

QFileInfo 类提供独立于系统的文件信息。

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

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

#include <QFileInfo>

(别忘了加上对应的#include-statement)

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()-function 的文档说:

如果文件存在则返回true;否则返回 false。

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

这并不准确。应该是:

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


TL;DR

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

#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 的 DR >=5.2

(使用 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 &amp;path)中的代码可以进一步简化为:return checkFile.exists() &amp;&amp; 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)。看看链接代码sn-p,我希望能解释我的意思。
  • 但是等等,它变得更糟哈哈:i.imgur.com/5Hds4kA.png "file" sigh
【解决方案2】:

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

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

如果您希望它仅在文件存在时返回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() &amp;&amp; !QDir(path).exists()。我已经编辑了我的答案以添加它。
【解决方案3】:

您发布的代码是正确的。很有可能是其他地方出了问题。

试试这个:

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

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

【讨论】:

    【解决方案4】:

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

    #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 很难检查数据库是否存在,因为如果不存在它会自动创建一个新数据库。

    【讨论】:

    • 这与问题有什么关系?
    【解决方案5】:

    我会完全跳过使用 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 支持的平台,这些大多是有争议的。
    猜你喜欢
    • 2013-09-19
    • 2013-06-04
    • 2020-03-20
    • 2011-01-15
    • 2010-09-16
    • 1970-01-01
    • 2011-04-07
    • 2017-04-24
    • 2016-10-11
    相关资源
    最近更新 更多