scorpion
scorpion

Reputation: 97

QFile isn't opening

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!

Upvotes: 0

Views: 1402

Answers (1)

Chris Browet
Chris Browet

Reputation: 4266

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

Upvotes: 2

Related Questions