Txt file data to Qmap



  • Dear Qt friends, I am try to read txt file info to QMap as following code.
    The text file has IP info separated by new line,
    Name: IP info
    IP address :10.1.1.10
    Port: 8888
    the lst output is ("Name:IP info", ..... ," ")
    on the Qmap insert getting error "no matching function for call to
    'QMap<long long int, QString>::value(const QString&)'

    Am i miss something, i saw many similar error posted on forum but as get how to solve this (i an new to Qt) . Any thoughts/ suggestion highly useful.

    void Save()
    {
        int c = -1;
        QFile inputFile("C:\\Users\\IP_info.txt");
        QMap<qint64,QString> map;
      
    
        if (!inputFile.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            qDebug()<<"Could not open input file";
            return;
    
         }
        while (!inputFile.atEnd())
        {
               const QByteArray alldata = inputFile.readAll();
               const QString str(alldata);
               const QStringList lst = QString(alldata).split('\n');
    
               qDebug()<<lst;
              // const QString str(line);
              // const QStringList lst = line.split('\n');
    
               for (int i=0; i<lst.size(); i++)
                {
                   if (!map.value(lst.at(i)))  //getting error 
                 //    if (!map.containslst.at(i))) 
                    {
                        map.insert(lst.at(i),++c);
                    }
                }
               qDebug() << map;
                inputFile.close();
            }
    }
    

  • Qt Champions 2017

    hi
    Your map is
    QMap<qint64, QString>
    So int as key, and string as value.

    and you try to call so
    http://doc.qt.io/qt-5/qmap.html#value
    !map.value( lst.at(i) )
    but is lst.at(i) not a string and your key is qint64 ?
    So should it not be
    http://doc.qt.io/qt-5/qmap.html#contains
    if you try checking if u already have key ?
    also, is
    lst.at(i) ment to be the qint64? but just as text ?

    else it seems a bit reverse to me.



  • yes, you are right it was reversed. The following code store values in QMap. now i want the QMap key, String (value) into a another txt file. (Copy_IP_Info. txt) After written into new txt file (Copy_IP_Info) , the program objective

    1. have to check in certain interval of original IP_Info.txt
    2. If any info (value) changed in original file like IP address changed but Port number is same then only IP address should be updated to copy_IP_info file.

    Is it too complex? STL containers are new to me.. am i miss anything.. .any suggestion highly useful.

    
     for (int i=0; i<lst.size(); i++)
                {
    
    
                  // if (!map.value(lst.at(i)))
                    {
                        map.insert(++c,lst.at(i));
                    }
    
                }
    
    QMap((0, "Name:Follower2")(1, "IP_address:192.168.27.38")(2, "PORT_number:88888")(3, "Time_stamp:25th June 2018:3.00am")(4, "Version:1.0")(5, ""))
    r code here
    

  • Qt Champions 2017

    @Chandras002
    But its not really possible to update in the middle of a text file without rewriting the whole file.
    so unless its sort of a binary fixed record file, you will have to write the other data too.
    Im not really sure in what way Copy_IP_Info is different original IP_Info.txt
    except maybe its other format?



  • @mrjj : Hi, I got your point. Can not update the particular string... have to write whole file. Copy_IP_info.txt can be LOG file.
    In case, i want to append the latest data from IP_Info.txt for every 10 min with time stamp.. So, know the connection status details. Any thoughts?
    Thanks,

    Cheers!
    chandra


  • Moderators

    @Chandras002 If you want to append at the end of the file then there is no need to overwrite the whole file, just open it in append mode.

    file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.