Click here to Skip to main content
15,885,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to read data from Multiple .txt files in Qt. This how I am doing it



void MainWindow::on_pushButton_3_clicked()
{

 QString path = "C:/MyDevelopment/readfiles";
 QDir dir(path);



 QStringList filters;
 filters << "*.txt";

 foreach ( QString fileName, dir.entryList(filters, QDir::Files) )
 {

     QFile readFile(fileName);
     if(!readFile.open(QIODevice::ReadOnly | QIODevice::Text ) )
      {
        qDebug("Failed to read file.....");
        //return ;
      }
      QTextStream in(&fileName);
      while (!in.atEnd())
      {
          QString line = in.readLine();
           qDebug() << line;
      }

 }

I have 4 .txt file in the mentioned folder. I am always getting msg "Failed to read file.." for all files.
would be nice if someone can highlight what i am doing wrong
Posted
Updated 24-May-18 2:17am

I would try to call readfile.errorString()[^].
 
Share this answer
 
Did you checked the file names you are trying to open?

If you do so, you might notice that those are the plain file names without path. Then QFile tries to open the file in the current directory which is probably not the one passed to QDir.

To get the full names with path you must use QDir::entryInfoList:
foreach (QFileInfo fileInfo, dir.entryInfoList(filters, QDir::Files) )
{
    QFile readFile(fileInfo.absoluteFilePath());
    // ...
 
Share this answer
 
Comments
Ramiien 19-Nov-15 7:07am    
with QFileinfo control is not coming inside the foreach loop. ?

foreach (QFileInfo fileInfo, dir.entryInfoList(filters, QDir::Files) )
Jochen Arndt 19-Nov-15 7:22am    
Just tested it here and it is working (on a Pi with a suitable dir and mask):

QString path = "/home/pi/ArgosTest";
QDir dir(path);
QStringList filters;
filters << "*.sh";
foreach ( QFileInfo fileInfo, dir.entryInfoList(filters, QDir::Files) )
{
qDebug(fileInfo.absoluteFilePath().toLocal8Bit());
}

Output:
/home/pi/ArgosTest/main.sh
/home/pi/ArgosTest/shell.sh
/home/pi/ArgosTest/test.sh
/home/pi/ArgosTest/ts-calibrate.sh
/home/pi/ArgosTest/ts-test.sh

Ramiien 20-Nov-15 4:36am    
i posted my code in solution 3. why its not reading the file content??
Jochen Arndt 20-Nov-15 5:01am    
The code from solution 3 is not working? Then ask the poster of that solution.

Oh, that's you.

You should not post solutions that does not answer and solve the question (because they aren't a solution and may be therefore reported and removed). It is also impolite to post a solution based on other solutions and accepting that as an answer instead.

You have two options:

1. Edit your question, add the new code and explain what problems you have now. But don't delete parts of the original question or modify the original code. Otherwise the relation between existing answers and questions get lost. It is also good style to indicate what has been changed (e.g. by inserting something like "[EDIT]" in front of the added content). This applies also to answers (you have edited yours without those indications)

2. Raise a new question.

"why its not reading the file content?"
I don't know. Be informative: Where does the problem occur (e.g. opening, reading)? Is there some kind of error message? Are there compiler warnings? What is the debug output?

When providing these information, it is much simpler to locate the problem. Even you might find it yourself when answering these questions. Don't expect anyone here to compile your code.

Finally, you should delete your "solution".
QString path = "C:/MyDevelopment/readfiles";
    QDir dir(path);
    QStringList filters;
    filters << "*.txt";

    foreach (QFileInfo fileInfo, dir.entryInfoList(filters, QDir::Files))
    {
       QString fileName = fileInfo.absoluteFilePath();
       //qDebug(fileInfo.absoluteFilePath().toLocal8Bit());
       QFile readFile(fileInfo.absoluteFilePath());
       if(!readFile.open(QIODevice::ReadOnly | QIODevice::Text ) )
             {
               qDebug("Failed to read file.....");
               //return ;
             }
             QTextStream in(&fileName);
             while (!in.atEnd())
             {
                 QString line = in.readLine();
                  qDebug() << line;
             }
    }

output is file path still not reading the files. I am sure i am doing something wrong as usuall...
"C:/MyDevelopment/readfiles/fyfile3.txt"
 
Share this answer
 
v2
"C:/MyDevelopment/readfiles/fyfile3.txt"

change the path to the below and try
"C:\\MyDevelopment\\readfiles\\fyfile3.txt"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900