在Qt程序中,你可以使用QFileInfo类来判断指定路径是文件还是文件夹。以下是一个示例代码:
#include <QFileInfo>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString path = "/path/to/file_or_folder";
QFileInfo fileInfo(path);
if (fileInfo.isFile()) {
qDebug() << "指定路径是一个文件";
} else if (fileInfo.isDir()) {
qDebug() << "指定路径是一个文件夹";
} else {
qDebug() << "指定路径既不是文件也不是文件夹";
}
return a.exec();
}
你只需要将/path/to/file_or_folder
替换为你想要检查的具体路径即可。根据isFile()
和isDir()
方法的返回值,可以判断指定路径是否为文件或者文件夹。
内容由零声教学AI助手提供,问题来源于学员提问