C++ 如何检查c++;
如何检查Qt中是否存在给定路径中的文件 我目前的代码如下:C++ 如何检查c++;,c++,qt,file-exists,C++,Qt,File Exists,如何检查Qt中是否存在给定路径中的文件 我目前的代码如下: QFile Fout("/Users/Hans/Desktop/result.txt"); if(!Fout.exists()) { eh.handleError(8); } else { // ...... } 但是,当我运行代码时,它不会给出handleError中指定的错误消息,即使路径中提到的文件不存在。我将完全跳过使用Qt中的任何内容,而只使用旧标准access: if (0==access("
QFile Fout("/Users/Hans/Desktop/result.txt");
if(!Fout.exists())
{
eh.handleError(8);
}
else
{
// ......
}
但是,当我运行代码时,它不会给出
handleError
中指定的错误消息,即使路径中提到的文件不存在。我将完全跳过使用Qt中的任何内容,而只使用旧标准access
:
if (0==access("/Users/Hans/Desktop/result.txt", 0))
// it exists
else
// it doesn't exist
您发布的代码是正确的。很有可能是其他方面出了问题 试着说:
qDebug() << "Function is being called.";
qDebug()
我会使用QFileInfo
-class()-这正是它的用途:
QFileInfo类提供独立于系统的文件信息
QFileInfo提供有关文件名和位置(路径)的信息
在文件系统中,它的访问权限以及它是目录还是
符号链接等。文件大小和上次修改/读取时间
也可提供。QFileInfo还可用于获取有关
Qt资源
这是检查文件是否存在的源代码:
#include <QFileInfo>
还要考虑:您是只想检查路径是否存在(exists()
),还是还要确保这是一个文件而不是一个目录(isFile()
)
小心:exists()函数的文档说明:
如果文件存在,则返回true;否则返回false
注意:如果文件是指向不存在文件的符号链接,则返回false
这不准确。应该是:
如果路径(即文件或目录)存在,则返回true;否则返回false
TL;DR
(使用上述函数的较短版本,可节省几行代码)
这就是我检查数据库是否存在的方式:
#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 !";
}
#包括
#包括
#包括
#包括
#包括
QString db_path=“/home/serge/Projects/sqlite/users_admin.db”;
QSqlDatabase db=QSqlDatabase::addDatabase(“QSQLITE”);
db.setDatabaseName(db_路径);
如果(QFileInfo::存在(db_路径))
{
bool ok=db.open();
如果(确定)
{
qDebug()您可以使用QFileInfo::exists()
方法:
#include <QFileInfo>
if(QFileInfo("C:\\exampleFile.txt").exists()){
//The file exists
}
else{
//The file doesn't exist
}
@Styne666:据我所知,Windows上的每一个编译器都支持access
——当然是MS和gcc端口。Intel使用支持它的MS库,Comeau使用后端编译器库。感谢你让我做我的研究。我接受这一点,但考虑到我仍然认为坚持使用Qt“的选项(如果您有一个Qt项目)是一个更好的解决方案。@Styne666:我根本不确定Qt是否提供了任何东西来解决setuid/setgid程序的问题,这似乎是唯一重要的问题。他们就“跨平台”的含义进行了争论。”以及需要关注哪些标准,但如果我们只关注Qt支持的平台,那么这些都是没有实际意义的问题。我认为下面的@mozzbozz可能有你的答案——不要忘记接受/给出点:)只是一个建议,函数bool fileExists中的代码(const QString&path)
可以进一步简化为:返回checkFile.exists()&&checkFile.isFile();
@mozzbozz@Dreamer谢谢你的评论。当然你是对的,尽管这也是一个品味的问题。我也添加了你的版本(我将在这里留下较长的版本,因为它可能更容易让初学者理解)@kayleeFrye_onDeck小心点,你的编辑不是真的正确。文档说静态函数更快是对的。但是,文档对函数的实际功能并不精确。存在函数(静态或非静态)仅检查路径是否存在,而不检查是否存在文件。因此,如果存在具有给定路径的目录,则您的建议也将返回true
!(刚刚使用Qt 5.10在我的系统上进行了测试)@kayleeFrye\u onDeck-Sure(在Windows下使用Qt 5.6进行了测试):-->是的,我认为这是一个误解。我的意思是如果给定路径是目录,exists
函数(static
或not)返回true
。但是问题是“如何检查文件是否存在”(而不是目录)请看一看链接的代码片段,我希望这能解释我的意思。但是,等一下,情况会变得更糟哈哈:“文件”叹息小心:如果你给出了目录的路径,你的源代码也会返回true
,即使它是“仅”文件。OP要求检查文件,而不是路径。@mozzbozz如果希望它返回false
如果路径存在但为文件夹,则可以执行QFileInfo(path).exists()&&&!QDir(path).exists()
。我已编辑了我的答案以添加该选项。这与问题有何关系?
#include <QFileInfo>
// check if path exists and if yes: Is it a file and no directory?
bool fileExists = QFileInfo::exists(path) && QFileInfo(path).isFile();
#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 !";
}
#include <QFileInfo>
if(QFileInfo("C:\\exampleFile.txt").exists()){
//The file exists
}
else{
//The file doesn't exist
}
#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
}