I'm opening a directory, then take files with *.cpp
and *.h
extensions. Then I try to read them line by line in order to scan for some info and write it in another file, but while
returns true
and never starts.
QString output = "out.txt";
QFile file(output);
if (QFile(output).exists())
{
QFile::remove(output);
}
if (file.open(QIODevice::ReadWrite))
{
QTextStream out(&file);
out << directory << endl;
QDir dir(directory);
dir.setFilter(QDir::Files);
QFileInfoList list = dir.entryInfoList();
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
if (fileInfo.suffix() == "cpp" || fileInfo.suffix() == "h")
{
out << fileInfo.fileName() << endl;
QFile target(fileInfo.fileName());
if (target.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream in(&target);
while(!in.atEnd())
{
QString str = in.readLine();
out << str << endl;
}
}
}
}
}
system("notepad.exe out.txt");
I was trying to read files in the local directory, because I was trying to open them by their names instead of their paths. Here's the solution:
QFile target(fileInfo.fileName()); -> QFile target(fileInfo.absoluteFilePath());