Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. comaprision of the file values
Qt 6.11 is out! See what's new in the release blog

comaprision of the file values

Scheduled Pinned Locked Moved Unsolved C++ Gurus
4 Posts 4 Posters 1.8k Views 2 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.
  • A Offline
    A Offline
    anuj nogja
    wrote on last edited by
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by
      #2

      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.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      2
      • A anuj nogja

        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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • Paul ColbyP Offline
          Paul ColbyP Offline
          Paul Colby
          wrote on last edited by
          #4

          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.

          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