Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether file exists in Qt in c++

How do I check whether a file exists in a given path or not in 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.

like image 490
user1322915 Avatar asked Apr 23 '12 01:04

user1322915


People also ask

How do you check the file is present or not in Qt?

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.

How do I create a folder in Qt?

The only way I was able to create a folder was right-click on the 'MyApp' root folder in QtCreator, select 'Add New' and choose to add a new QML file. At that point, it brings up a file Browse dialog and you can navigate to the folder you created and drop the file in there.


2 Answers

(TL;DR at the bottom)

I would use the QFileInfo-class (docs) - this is exactly what it is made for:

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.

This is the source code to check whether a file exists:

#include <QFileInfo> 

(don't forget to add the corresponding #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())?

Be careful: The documentation of the exists()-function says:

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.

This is not precise. It should be:

Returns true if the path (i.e. file or directory) exists; otherwise returns false.


TL;DR

(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

(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)

#include <QFileInfo>  // check if path exists and if yes: Is it a file and no directory? bool fileExists = QFileInfo::exists(path) && QFileInfo(path).isFile(); 
like image 86
mozzbozz Avatar answered Sep 21 '22 00:09

mozzbozz


You can use the QFileInfo::exists() method:

#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 file exists and false if the path exists but is a folder, you can combine it with 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 } 
like image 40
Donald Duck Avatar answered Sep 22 '22 00:09

Donald Duck