C++ 如何在c ++中检查Qt中是否存在文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10273816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check whether file exists in Qt in c++
提问by user1322915
How do I check whether a file exists in a given path or not in Qt?
如何在Qt中检查给定路径中是否存在文件?
My current code is below:
我当前的代码如下:
QFile Fout("/Users/Hans/Desktop/result.txt");
if(!Fout.exists())
{
eh.handleError(8);
}
else
{
// ......
}
But when I run the code it is not giving the error message specified in handleError
even though the file I mentioned in the path does not exist.
但是当我运行代码时,handleError
即使我在路径中提到的文件不存在,它也没有给出指定的错误消息。
回答by mozzbozz
(TL;DR at the bottom)
(TL;底部的DR)
I would use the QFileInfo
-class (docs) - this is exactly what it is made for:
我会使用QFileInfo
-class ( docs) - 这正是它的用途:
The QFileInfo class provides system-independent file information.
QFileInfo provides information about a file's name and position (path) in the file system, its access rights and whether it is a directory or symbolic link, etc. The file's size and last modified/read times are also available. QFileInfo can also be used to obtain information about a Qt resource.
QFileInfo 类提供与系统无关的文件信息。
QFileInfo 提供有关文件系统中文件的名称和位置(路径)、访问权限以及它是目录还是符号链接等信息。文件的大小和上次修改/读取时间也可用。QFileInfo 还可用于获取有关 Qt 资源的信息。
This is the source code to check whether a file exists:
这是检查文件是否存在的源代码:
#include <QFileInfo>
(don't forget to add the corresponding #include
-statement)
(不要忘记添加相应的#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;
}
}
Also consider: Do you only want to check if the path exists (exists()
) or do you want to also make sure that this is a file and not a directory (isFile()
)?
还要考虑:您只想检查路径是否存在 ( exists()
) 还是还想确保这是一个文件而不是目录 ( isFile()
)?
Be careful: The documentation of the exists()
-function says:
小心:exists()
-function的文档说:
Returns true if the file exists; otherwise returns false.
Note: If file is a symlink that points to a non-existing file, false is returned.
如果文件存在则返回真;否则返回false。
注意:如果 file 是指向不存在文件的符号链接,则返回 false。
This is not precise.It should be:
这并不准确。它应该是:
Returns true if the path (i.e. file or directory) exists; otherwise returns false.
如果路径(即文件或目录)存在则返回真;否则返回false。
TL;DR
TL; 博士
(with shorter version of the function above, saving a few lines of code)
(使用上面函数的较短版本,节省了几行代码)
#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;DR for Qt >=5.2
TL; Qt >=5.2 的 DR
(using exists
as a static
which was introduce in Qt 5.2; the docs say the static function is faster, though I'm not sure this is still the case when also using the isFile()
method; at least this is a one-liner then)
(exists
作为static
在 Qt 5.2 中引入的使用;文档说静态函数更快,但我不确定在使用该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();
回答by Donald Duck
You can use the QFileInfo::exists()
method:
您可以使用以下QFileInfo::exists()
方法:
#include <QFileInfo>
if(QFileInfo("C:\exampleFile.txt").exists()){
//The file exists
}
else{
//The file doesn't exist
}
If you want it to return true
only if the fileexists and false
if the path exists but is a folder, you can combine it with QDir::exists()
:
如果您希望它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
}
回答by Anthony
The code you've posted is correct. Chances are that something else is wrong.
您发布的代码是正确的。有可能是其他事情出了问题。
Try putting this:
试着把这个:
qDebug() << "Function is being called.";
inside of your handleError function. If the above message prints, you know something else is the problem.
在您的 handleError 函数中。如果打印出上述消息,您就知道还有其他问题。
回答by Essetee
That's how I check if the database exists:
这就是我检查数据库是否存在的方式:
#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 !";
}
With SQLite
it's difficult to check if the database exists, because it automatically creates a new database if it doesn't exist.
随着SQLite
很难检查数据库是否存在,因为它会自动创建一个新的数据库,如果它不存在。
回答by Jerry Coffin
I would skip using anything from Qt at all, and just use the old standard access
:
我会跳过使用 Qt 中的任何东西,只使用旧标准access
:
if (0==access("/Users/Hans/Desktop/result.txt", 0))
// it exists
else
// it doesn't exist