0
votes

I wrote the following code for retreiving data from a file(which already exists and permision is also given because i am on Windows OS), and creates items to display data fragments in a List, but the list won't show any thing. More over I figured out even when the file wasn't created, the FILE.EXISTS() function returned true. why is this so?

 void MainWindow::on_listWidget_itemClicked(QListWidgetItem *item)  
 {
     ui->listWidget_2->clear();
     QListWidgetItem *itm=new QListWidgetItem;
     ui->commentbutton->setEnabled(true);

     QFile files("E:/"+QString::number(ui->listWidget->currentRow())+"com.txt");

     if(files.exists())
     {
         if(!files.open(QFile::ReadOnly | QFile::Text))
         {
              QMessageBox::warning(this,"File Access!!!","The File containing data of      the Items and Comments can't be acessed",QMessageBox::Ok);
              return;
         }
         QTextStream in(&files);
         QString data(in.readLine());
         int x=0;

         QString temp;

         for(int i=0;;i++)
          {
             if(data.at(i)!='@' && data.at(i+1)!='#')
             {
                 temp[x]=data.at(i);
                 x++;
             }
             else
                 if(data.at(i)=='@' && data.at(i+1)=='#')
                 {
                     x=0;
                     i++;
                     itm->setText(temp);
                     ui->listWidget_2->addItem(itm);
                 }
             if(data.end())
                 break;
         }
         files.close();
     }

the path at which the files are generated displays:0 & 1 are the files containing the data stored in items and 0com is the comment file associated with 0 item file

This is the data stored in 0com.txt file (comment file): NewYork@#London@# Thanks for your time!

1
Doesn't really make sense. How did you make sure that the path generated in your code is actually what you think it is? Could you update your code to show the generated path, show that?Mat
This is a fixed path. I don't change this path for file storage. The only thing that changes is related to the item number in the list so that the comments of the item which is currently selected, are displayed in the other list.scorpion
What does files.error() return when the open() fails? qt-project.org/doc/qt-4.8/qfile.html#erroruser362638
I'm talking about the QFile files(...) line. The thing in the parenthesis is a generated path. Please display that.Mat
check out i have displayed the required thing!scorpion

1 Answers

2
votes

1) 0com.txt actually exists. For what current row number in ui->listWidget do you have a "false" files.exists()?

2) data.end() returns a STL-style iterator, while your are incrementing by index. use

if(i>= data.size())
    break;

3) Please show the content of "0com.txt" for further debugging