comaprision of the file values



  • Hi All,

    I have one file of format like this (file1.txt):
    s1=ABCDE
    s2=ERTY 1
    s3=PRTYU 2
    This implies that value of s1 is ABCDE, value of s2 is ERTY 2 and like this.

    I have another file (file2.txt):
    s2=EEEE
    s1=ABCD23

    Requirement is to compare the value of s1 of file2 with s1 of file1 and so on..... and to display that values are same or not.

    Kindly suggest how this can be achieved...

    Regards,
    Anuj



  • Your files looks much like INI files actually. If so, use a INI parser and check if the key "s1" of file one matches the "s1" key in file2.

    If your files are not compatible with INI format, just implement your own parsing method. I'd store each key=value entry in a QMap. Then it's simply a matter of checking the value of "s1" key in both QMap instances. Note that this method also applies to real INI files.


  • Moderators

    @anuj-nogja With ini parser @JohanSolo means QSettings class.



  • Just to demonstrate what @JohanSolo and @jsulm suggested:

        const QSettings file1("file1.txt", QSettings::IniFormat);
        qDebug() << "file1 keys" << file1.allKeys();
    
        const QSettings file2("file2.txt", QSettings::IniFormat);
        qDebug() << "file2 keys" << file2.allKeys();
    
        foreach (const QString &key, file1.allKeys()) {
            if (!file2.contains(key))
                qDebug() << "in file1 only:" << key;
        }
    
        foreach (const QString &key, file2.allKeys()) {
            if (!file1.contains(key))
                qDebug() << "in file2 only:" << key;
        }
    
        foreach (const QString &key, file1.allKeys()) {
            if ((file2.contains(key)) && (file1.value(key) != file2.value(key)))
                qDebug() << "file1 and file2 differ:" << key << file1.value(key) << file2.value(key);
        }
    

    Output (with your sample files above):

    file1 keys ("s1", "s2", "s3")
    file2 keys ("s1", "s2")
    in file1 only: "s3"
    file1 and file2 differ: "s1" QVariant(QString, "ABCDE") QVariant(QString, "ABCD23")
    file1 and file2 differ: "s2" QVariant(QString, "ERTY 1") QVariant(QString, "EEEE")
    

    Cheers.


Log in to reply
 

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