Important: Please read the Qt Code of Conduct -

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 :
    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 (! | QIODevice::Text))
            qDebug()<<"Could not open input file";
        while (!inputFile.atEnd())
               const QByteArray alldata = inputFile.readAll();
               const QString str(alldata);
               const QStringList lst = QString(alldata).split('\n');
              // const QString str(line);
              // const QStringList lst = line.split('\n');
               for (int i=0; i<lst.size(); i++)
                   if (!map.value(  //getting error 
                 //    if (! 
               qDebug() << map;

  • Lifetime Qt Champion

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

    and you try to call so
    !map.value( )
    but is not a string and your key is qint64 ?
    So should it not be
    if you try checking if u already have key ?
    also, is 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(
    QMap((0, "Name:Follower2")(1, "IP_address:")(2, "PORT_number:88888")(3, "Time_stamp:25th June 2018:3.00am")(4, "Version:1.0")(5, ""))
    r code here

  • Lifetime Qt Champion

    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?


  • Lifetime Qt Champion

    @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. | QIODevice::Text | QIODevice::Append);

Log in to reply