Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. having trouble reading from textstream

having trouble reading from textstream

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 161 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kook
    wrote on last edited by
    #1

    Greetings..
    I am having some problem reading from the text stream
    in the below code first i am reading the whole content of a file to a string and check if the pressed item is present if it is present then i will proceed further

    if i remove int pos = str.indexOf(item->text()); and datafile.seek(pos);
    these two lines it will not enter the while loop
    why so

    i dont want to seek to any position i want to read from the beginning only but it is entering to the while loop only if i add above mentioned lines

    void MainWindow::on_listWidget_itemPressed(QListWidgetItem *item)
    {
        
        QFile datafile("/home/msg.txt");
    
        if(!datafile.open(QIODevice::ReadOnly))
        {
            qDebug() << "file not opened";
        }
    
        QString line;
        QTextStream in(&datafile);
        QString str = in.readAll();
    
        if (str.contains(item->text()))
        {
            int pos = str.indexOf(item->text());
            datafile.seek(pos);
            
            while (in.readLineInto(&line))
            {
                
                //skipping commented line
                if (line[0] =='#')
                {
                    continue;
                }
                QStringList list = line.split(QLatin1Char(':'), QString::SkipEmptyParts);
                qDebug()<<list;
                if (list[0] == item->text())
                {
                    msgname = list[0];
                }
    
                if( list[0] == "MSG_ID")
                {
                    msgid = list[1].toInt();
                    qDebug()<< msgid;
                }
                ui->textEdit->setText(QString("Messagename = %1").arg(msgname));
                ui->textEdit->append(QString("MessageId = %1").arg(msgid));
            }
        }
    
    }
    

    Thanks in advance

    JonBJ 1 Reply Last reply
    0
    • K kook

      Greetings..
      I am having some problem reading from the text stream
      in the below code first i am reading the whole content of a file to a string and check if the pressed item is present if it is present then i will proceed further

      if i remove int pos = str.indexOf(item->text()); and datafile.seek(pos);
      these two lines it will not enter the while loop
      why so

      i dont want to seek to any position i want to read from the beginning only but it is entering to the while loop only if i add above mentioned lines

      void MainWindow::on_listWidget_itemPressed(QListWidgetItem *item)
      {
          
          QFile datafile("/home/msg.txt");
      
          if(!datafile.open(QIODevice::ReadOnly))
          {
              qDebug() << "file not opened";
          }
      
          QString line;
          QTextStream in(&datafile);
          QString str = in.readAll();
      
          if (str.contains(item->text()))
          {
              int pos = str.indexOf(item->text());
              datafile.seek(pos);
              
              while (in.readLineInto(&line))
              {
                  
                  //skipping commented line
                  if (line[0] =='#')
                  {
                      continue;
                  }
                  QStringList list = line.split(QLatin1Char(':'), QString::SkipEmptyParts);
                  qDebug()<<list;
                  if (list[0] == item->text())
                  {
                      msgname = list[0];
                  }
      
                  if( list[0] == "MSG_ID")
                  {
                      msgid = list[1].toInt();
                      qDebug()<< msgid;
                  }
                  ui->textEdit->setText(QString("Messagename = %1").arg(msgname));
                  ui->textEdit->append(QString("MessageId = %1").arg(msgid));
              }
          }
      
      }
      

      Thanks in advance

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @kook said in having trouble reading from textstream:

      QString str = in.readAll();
      ...
      while (in.readLineInto(&line))
      

      If you have already done a readAll() from the stream, where do you expect a subsequent readLineInto() to start reading from? Either do your work on the str containing all the read data or don't do that and do the readLineInto(), not both. Or if you do want to use both as you have shown, it is not surprising you have to go datafile.seek(pos); to reposition the reader; but I would either do everything off the str or everything off the in.

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved