一个txt文件中每一行是一个文件的绝对路径使用Qt的语法写一个函数按行读取txt文件判断每一个文件是否存在。
可以使用QFile类来判断文件是否存在。下面是一个示例的函数实现:
#include <QFile>
#include <QStringList>
void checkFileExistence(const QString &filePath)
{
QFile file(filePath);
if (file.exists()) {
qDebug() << "File" << filePath << "exists.";
} else {
qDebug() << "File" << filePath << "does not exist.";
}
}
void checkFilesInTxt(const QString &txtFilePath)
{
QFile txtFile(txtFilePath);
if (!txtFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Failed to open" << txtFilePath;
return;
}
QTextStream in(&txtFile);
while (!in.atEnd()) {
QString filePath = in.readLine();
checkFileExistence(filePath);
}
txtFile.close();
}
在checkFilesInTxt函数中,首先打开指定的txt文件并创建QTextStream对象,然后逐行读取文件路径并调用checkFileExistence函数来判断文件是否存在。checkFileExistence函数使用QFile的exists方法来判断文件是否存在,并输出相应的信息。
原文地址: https://www.cveoy.top/t/topic/i5V0 著作权归作者所有。请勿转载和采集!