Qt 判断文件、文件夹是否存在的方法

本文详细对比了Qt中isFile、isDir与exists方法的区别及应用场景,提供了准确判断文件与文件夹存在的方法,并给出了实用的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一直对Qt的isFile、isDir、exists这几个方法感到混乱,不知道到底用哪个,网上搜了下资料,也是用这几个方法

但是都没有对其深究,经过测试发现会存在问题,先看看下面的测试代码

[cpp]  view plain  copy
  1. {  
  2.     QFileInfo fi("C:/123");                     // 目录存在  
  3.     qDebug() << fi.isFile();                    // false  
  4.     qDebug() << fi.isDir();                     // true  
  5.     qDebug() << fi.exists();                    // true  
  6.     qDebug() << fi.isRoot();                    // false  
  7.     qDebug() << QFile::exists("C:/123");        // true  
  8.     qDebug() << QDir("C:/123").exists();        // true  
  9.   
  10.     fi.setFile("C:/ABC");                       // 目录不存在  
  11.     qDebug() << fi.isFile();                    // false  
  12.     qDebug() << fi.isDir();                     // false  
  13.     qDebug() << fi.exists();                    // false  
  14.     qDebug() << fi.isRoot();                    // false  
  15.     qDebug() << QFile::exists("C:/ABC");        // false  
  16.     qDebug() << QDir("C:/ABC").exists();        // false  
  17.   
  18.     fi.setFile("C:/");                          // 存在的驱动器  
  19.     qDebug() << fi.isFile();                    // false  
  20.     qDebug() << fi.isDir();                     // true  
  21.     qDebug() << fi.exists();                    // true  
  22.     qDebug() << fi.isRoot();                    // true  
  23.     qDebug() << QFile::exists("C:/");           // true  
  24.     qDebug() << QDir("C:/").exists();           // true  
  25.   
  26.     fi.setFile("Z:/");                          // 不存在的驱动器  
  27.     qDebug() << fi.isFile();                    // false  
  28.     qDebug() << fi.isDir();                     // false  
  29.     qDebug() << fi.exists();                    // false  
  30.     qDebug() << fi.isRoot();                    // false  
  31.     qDebug() << QFile::exists("Z:/");           // false  
  32.     qDebug() << QDir("Z:/").exists();           // false  
  33.   
  34.     fi.setFile("C:/123.lnk");                   // 快捷方式存在且指向的文件也存在  
  35.     qDebug() << fi.isFile();                    // true  
  36.     qDebug() << fi.isDir();                     // false  
  37.     qDebug() << fi.exists();                    // true  
  38.     qDebug() << fi.isRoot();                    // false  
  39.     qDebug() << QFile::exists("C:/123.lnk");    // true  
  40.     qDebug() << QDir("C:/123.lnk").exists();    // false  
  41.   
  42.     fi.setFile("C:/456.lnk");                   // 快捷方式存在但指向的文件不存在  
  43.     qDebug() << fi.isFile();                    // false  
  44.     qDebug() << fi.isDir();                     // false  
  45.     qDebug() << fi.exists();                    // false  
  46.     qDebug() << fi.isRoot();                    // false  
  47.     qDebug() << QFile::exists("C:/456.lnk");    // false  
  48.     qDebug() << QDir("C:/456.lnk").exists();    // false  
  49. }  


 

可以看到,容易让人感到混乱的是exists方法,这个方法是通用的判断方法,可以看成是这样的表达式

exists() == (isFile() || isDir())

 

这也是我想说明的问题,网上一些博文中提出的判断文件或文件夹用exists方法是不严谨的
比如你的本意是判断文件是否存在,但文件不存在,而恰巧有个同名的文件夹,那么exists也会返回true。文件夹也是同理

 

根据上面的代码作出的一点总结

准确判断文件是否存在

1.用QFileInfo::isFile()方法

 

准确判断文件夹是否存在
1.用QFileInfo::isDir()方法
2.用QDir::exists()方法

 

不确定字符串是文件还是文件夹路径
1.用QFileInfo::exists()方法
2.用QFile::exists()方法


-------------------------------------------------------------------------------------------------------------------------

1. 判断文件夹是不是存在
参数说明:
QString fullPath;//文件夹全路径
/*方法1*/
bool isDirExist(QString fullPath)
{
    QDir dir(fullPath);
    if(dir.exists())
    {
      return true;
    }
    return false;
}
/*方法2*/
bool isDirExist(QString fullPath)
{
    QFileInfo fileInfo(fullPath);
    if(fileInfo.isDir())
    {
      return true;
    }
    return false;
}

2. 判断文件是不是存在
参数说明:
QString fullFileName;//文件全路径(包含文件名)
/*方法1*/
bool isFileExist(QString fullFileName)
{
    QFileInfo fileInfo(fileFullName);
    if(fileInfo.isFile())
    {
        return true;
    }
    return false;
}

3、判断文件或文件夹是不是存在(即不确定字符串是文件还是文件夹路径)
参数说明:
QString fullFilePath;//路径名
/*方法1*/
bool isFileExist(QString fullFilePath)
{
    QFileInfo fileInfo(fullFilePath);
    if(fileInfo.exists())
    {
        return true;
    }
    return false;
}
/*方法2*/
bool isFileExist(QString fullFilePath)
{
    QFile file(fullFilePath);
    if(file.exists())
    {
        return true;
    }
    return false;
}

4、判断文件夹是否存在,不存在则创建
/*方法1*/
bool isDirExist(QString fullPath)
{
    QDir dir(fullPath);
    if(dir.exists())
    {
      return true;
    }
    else
    {
       bool ok = dir.mkdir(fullPath);//只创建一级子目录,即必须保证上级目录存在
       return ok;
    }
}
/*方法1*/
bool isDirExist(QString fullPath)
{
    QDir dir(fullPath);
    if(dir.exists())
    {
      return true;
    }
    else
    {
       bool ok = dir.mkpath(fullPath);//创建多级目录
       return ok;
    }
}

判断给定文件夹是否存在以 `.m` 后缀的文件,您可以使用 QDir 类来进行文件夹操作,并结合正则表达式来进行文件匹配。以下是一个示例代码: ```cpp #include <QDir> #include <QRegularExpression> #include <QDebug> int main() { QString folderPath = "/path/to/folder"; // 使用 QDir 打开给定的文件夹路径 QDir folder(folderPath); // 构建正则表达式,匹配以 .m 结尾的文件名 QRegularExpression regex("\\.m$"); // 获取文件夹中的所有文件 QStringList files = folder.entryList(QDir::Files); // 遍历文件列表,查找匹配的文件 bool hasMFile = false; for (const QString& file : files) { if (regex.match(file).hasMatch()) { qDebug() << "Found .m file:" << file; hasMFile = true; break; } } // 判断是否找到 .m 文件 if (!hasMFile) { qDebug() << "No .m file found."; } return 0; } ``` 上述代码中,我们首先使用 QDir 类打开给定的文件夹路径。然后,我们构建了一个正则表达式 `\.m$`,它匹配以 `.m` 结尾的文件名。接下来,我们使用 `entryList` 函数获取文件夹中的所有文件,并存储在一个 QStringList 中。然后,我们遍历文件列表,通过正则表达式匹配文件名,如果找到匹配的文件,我们打印出文件名,并将 `hasMFile` 设置为 true。最后,我们根据 `hasMFile` 的值判断是否找到了以 `.m` 结尾的文件。 请注意,您需要将 `/path/to/folder` 替换为实际的文件夹路径。希望这能满足您的需求!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值