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. How to Read Value from a text file and Iterate?
Forum Updated to NodeBB v4.3 + New Features

How to Read Value from a text file and Iterate?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 1.3k Views 1 Watching
  • 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.
  • Y Offline
    Y Offline
    YDYD
    wrote on last edited by
    #1

    Hi all,

    I have a .txt file with 6 random integer as following layout:

    52
    241
    40
    180
    7
    120

    I used the following code to read from the text file.
    @void Dialog::loadInitValue()
    {
    QString list;
    QString path="/mnt/rd/hsv.txt";
    QFile valueHSV(path);
    if(!valueHSV.open(QIODevice::ReadOnly|QIODevice::Text))
    {
    qDebug() << "cannot open file for reading"<<endl;
    return;
    }

    QTextStream hsv(&valueHSV);
    while(!hsv.atEnd())
    {
        list.append(hsv.readLine());
        if(list.isNull())
            break;
         qDebug()<<list;
    }
    foreach(QString str, list)
    {
        int value = str.toInt();
        qDebug()<<value;
    }
    
    return;
    

    }@

    for "list", i will get:
    "52"
    "241"
    "40"
    "180"
    "7"
    "120"

    for "value", i will get:
    5
    2
    2
    4
    1
    4
    0
    1
    8
    0
    7
    1
    2
    0

    I would like to store each of the value into different variables.
    How could i achieve this?

    1 Reply Last reply
    0
    • IamSumitI Offline
      IamSumitI Offline
      IamSumit
      wrote on last edited by
      #2

      hii

      • Line 3 : -QString list;
        Update by QStringList list;

      • Line 16: -list.isNull());
        Update by list.isEmpty();

      hope it helps

      Be Cute

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        You can make it more simple if the file format given by is the only format.

        @ QString path="hsv.txt";
        QFile valueHSV(path);
        if(!valueHSV.open(QIODevice::ReadOnly|QIODevice::Text))
        {
        qDebug() << "cannot open file for reading"<<endl;
        return 1;
        }

        while(!valueHSV.atEnd()){
           QString val =  valueHSV.readLine();
           qDebug() << val.toInt();
        }@
        

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          Iterating through a QStringList should best be done with a const_iter variable.
          Or simply take the first element, and send via debug, but then you loose the data.

          Greetz, Jeroen

          1 Reply Last reply
          0

          • Login

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