C ++의 Qt에 파일이 있는지 확인하는 방법
Qt에서 파일이 주어진 경로에 있는지 여부를 어떻게 확인합니까?
내 현재 코드는 다음과 같습니다.
QFile Fout("/Users/Hans/Desktop/result.txt");
if(!Fout.exists())
{
eh.handleError(8);
}
else
{
// ......
}
그러나 코드를 실행 handleError
하면 경로에서 언급 한 파일이 존재하지 않더라도 지정된 오류 메시지 가 표시되지 않습니다.
(하단의 TL; DR)
나는 QFileInfo
-class ( docs )를 사용합니다 -이것이 정확히 만들어진 것입니다.
QFileInfo 클래스는 시스템 독립적 파일 정보를 제공합니다.
QFileInfo는 파일 시스템에서 파일의 이름과 위치 (경로), 액세스 권한, 디렉토리인지 심볼릭 링크인지 등에 대한 정보를 제공합니다. 파일의 크기와 마지막 수정 / 읽기 시간도 사용할 수 있습니다. QFileInfo를 사용하여 Qt 리소스에 대한 정보를 얻을 수도 있습니다.
파일이 존재하는지 확인하는 소스 코드입니다.
#include <QFileInfo>
(해당- #include
문 을 추가하는 것을 잊지 마십시오)
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;
}
}
또한 다음을 고려하십시오. 경로가 있는지 만 확인 하시겠습니까 ( exists()
) 아니면 이것이 디렉토리가 아닌 파일인지 확인 isFile()
하시겠습니까 ( )?
주의 : exists()
-function 의 문서는 다음과 같이 말합니다.
파일이 있으면 true를 반환합니다. 그렇지 않으면 거짓을 반환합니다.
참고 : file이 존재하지 않는 파일을 가리키는 심볼릭 링크 인 경우 false가 반환됩니다.
이것은 정확하지 않습니다. 그것은해야한다:
경로 (예 : 파일 또는 디렉터리)가 있으면 true를 반환합니다. 그렇지 않으면 거짓을 반환합니다.
TL; DR
(위의 함수의 짧은 버전을 사용하면 코드 몇 줄을 절약 할 수 있음)
#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
( Qt 5.2에서 소개 된 exists
a로 사용 static
; 문서는 정적 함수가 더 빠르다고 말하지만 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();
다음 QFileInfo::exists()
방법을 사용할 수 있습니다 .
#include <QFileInfo>
if(QFileInfo("C:\\exampleFile.txt").exists()){
//The file exists
}
else{
//The file doesn't exist
}
파일 이 있고 경로가 존재하지만 폴더 인 경우 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
}
게시 한 코드가 정확합니다. 다른 것이 잘못되었을 가능성이 있습니다.
이것을 넣어보십시오 :
qDebug() << "Function is being called.";
handleError 함수 내부. 위의 메시지가 인쇄되면 다른 문제가있는 것입니다.
이것이 데이터베이스가 있는지 확인하는 방법입니다.
#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.
I would skip using anything from Qt at all, and just use the old standard access
:
if (0==access("/Users/Hans/Desktop/result.txt", 0))
// it exists
else
// it doesn't exist
참고URL : https://stackoverflow.com/questions/10273816/how-to-check-whether-file-exists-in-qt-in-c
'IT TIP' 카테고리의 다른 글
색상 화 된 출력을 셸 리디렉션을 통해 캡처 할 수 있습니까? (0) | 2020.11.01 |
---|---|
bash에서 튜플을 반복합니까? (0) | 2020.11.01 |
'ABC'.replace ('B ','$` ')가 AAC를 제공하는 이유 (0) | 2020.11.01 |
SET READ_COMMITTED_SNAPSHOT ON은 얼마나 걸리나요? (0) | 2020.10.31 |
Javascript가 모든 웹 페이지의 소스를 읽을 수 있습니까? (0) | 2020.10.31 |