Qt 判斷檔案或資料夾是否存在及建立資料夾

2021-09-12 21:38:17 字數 1335 閱讀 7565

1. 判斷資料夾是不是存在

引數說明:

qstring fullpath;//資料夾全路徑

/方法1/

bool isdirexist(qstring fullpath)

return false;

}/方法2/

bool isdirexist(qstring fullpath)

return false;

}2. 判斷檔案是不是存在

引數說明:

qstring fullfilename;//檔案全路徑(包含檔名)

/方法1/

bool isfileexist(qstring fullfilename)

return false;

}3、判斷檔案或資料夾是不是存在(即不確定字串是檔案還是資料夾路徑)

引數說明:

qstring fullfilepath;//路徑名

/方法1/

bool isfileexist(qstring fullfilepath)

return false;

}/方法2/

bool isfileexist(qstring fullfilepath)

return false;

}4、判斷資料夾是否存在,不存在則建立

/方法1/

bool isdirexist(qstring fullpath)

else

}/方法1/

bool isdirexist(qstring fullpath)

else

}5、以下為摘錄的其他網路測試**

可以看到,容易讓人感到混亂的是exists方法,這個方法是通用的判斷方法,可以看成是這樣的表示式

exists() == (isfile() || isdir()) 

也就是說判斷檔案或資料夾是否存在單純用exists方法是不嚴謹的

比如你的本意是判斷檔案是否存在,但檔案不存在,而恰巧有個同名的資料夾,那麼exists也會返回true。資料夾也是同理

根據上面的**作出的一點總結

準確判斷檔案是否存在

1.用qfileinfo::isfile()方法 

準確判斷資料夾是否存在

1.用qfileinfo::isdir()方法

2.用qdir::exists()方法 

不確定字串是檔案還是資料夾路徑

1.用qfileinfo::exists()方法

2.用qfile::exists()方法

Qt 判斷檔案或資料夾是否存在及建立資料夾

1.判斷資料夾是不是存在 引數說明 qstring fullpath 資料夾全路徑 方法1 bool isdirexist qstring fullpath return false 方法2 bool isdirexist qstring fullpath return false 2.判斷檔案是不...

Qt 判斷檔案或資料夾是否存在及建立資料夾

1.判斷資料夾是不是存在 引數說明 qstring fullpath 資料夾全路徑 方法1 bool isdirexist qstring fullpath return false 方法2 bool isdirexist qstring fullpath return false 2.判斷檔案是不...

c 判斷檔案或資料夾是否存在

判斷檔案是否存在 bool isfileexist const cstring csfile 判斷資料夾是否存在 bool isdirexist const cstring csdir 判斷檔案或資料夾是否存在 bool ispathexist const cstring cspath 變變變變變種...