1. Determine if a folder exists Parameter description:
QString fullpath;//folder full path
/* Method 1*/
BOOL Isdirexist (QString FullPath)
{
Qdir dir (FullPath);
if (Dir.exists ())
{
return true;
}
return false;
}
/* Method 2*/
BOOL Isdirexist (QString FullPath)
{
Qfileinfo FileInfo (FullPath);
if (Fileinfo.isdir ())
{
return true;
}
return false;
}
2. Determine if the file is not present Parameter description:
QString fullfilename;//file full path (including file name)
/* Method 1*/
BOOL Isfileexist (QString fullfilename)
{
Qfileinfo FileInfo (filefullname);
if (Fileinfo.isfile ())
{
return true;
}
return false;
}
3. Determine if the file or folder is present (that is, you are not sure whether the string is a file or a folder path) Parameter description:
QString fullfilepath;//path name
/* Method 1*/
BOOL Isfileexist (QString Fullfilepath)
{
Qfileinfo FileInfo (Fullfilepath);
if (Fileinfo.exists ())
{
return true;
}
return false;
}
/* Method 2*/
BOOL Isfileexist (QString Fullfilepath)
{
QFile file (Fullfilepath);
if (File.exists ())
{
return true;
}
return false;
}
4, determine whether the folder exists, does not exist to create /* Method 1*/
BOOL Isdirexist (QString FullPath)
{
Qdir dir (FullPath);
if (Dir.exists ())
{
return true;
}
Else
{
bool OK = Dir.mkdir (fullPath);//create only one level subdirectory, that is, you must ensure that the parent directory exists
return OK;
}
}
/* Method 1*/
BOOL Isdirexist (QString FullPath)
{
Qdir dir (FullPath);
if (Dir.exists ())
{
return true;
}
Else
{
bool OK = Dir.mkpath (fullPath);//Create multilevel Directory
return OK;
}
}
5. The following is an excerpt of other network test code {
Qfileinfo fi ("c:/123"); Directory exists
Qdebug () << fi.isfile (); False
Qdebug () << fi.isdir (); True
Qdebug () << fi.exists (); True
Qdebug () << fi.isroot (); False
Qdebug () << qfile::exists ("c:/123"); True
Qdebug () << qdir ("c:/123"). exists (); True
Fi.setfile ("C:/123.lnk"); The shortcut exists and the file pointed to is present
Qdebug () << fi.isfile (); True
Qdebug () << fi.isdir (); False
Qdebug () << fi.exists (); True
Qdebug () << fi.isroot (); False
Qdebug () << qfile::exists ("C:/123.lnk"); True
Qdebug () << qdir ("C:/123.lnk"). exists (); False
Fi.setfile ("C:/456.lnk"); Shortcut exists but point to file does not exist
Qdebug () << fi.isfile (); False
Qdebug () << fi.isdir (); False
Qdebug () << fi.exists (); False
Qdebug () << fi.isroot (); False
Qdebug () << qfile::exists ("C:/456.lnk"); False
Qdebug () << qdir ("C:/456.lnk"). exists (); False
}
It can be seen that the Exists method, which is easy to confuse, is a common method of judging and can be seen as an expression
exists () = = (Isfile () | | isdir ())
In other words, it is not rigorous to judge whether a file or a folder exists simply by using the Exists method
For example, your intention is to determine whether the file exists, but the file does not exist, and happen to have a folder of the same name, then exists will also return true. The same is true for folders
A little summary based on the above code
Accurately determine if a file exists
1. Using the Qfileinfo::isfile () method
Accurately determine if a folder exists
1. Using the Qfileinfo::isdir () method
2. Using the Qdir::exists () method
Not sure if the string is a file or a folder path
1. Using the Qfileinfo::exists () method
2. Using the Qfile::exists () method
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.