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 make QFile read the whole line
Forum Updated to NodeBB v4.3 + New Features

How to make QFile read the whole line

Scheduled Pinned Locked Moved Unsolved General and Desktop
48 Posts 8 Posters 8.9k 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.
  • JohanSoloJ Offline
    JohanSoloJ Offline
    JohanSolo
    wrote on last edited by
    #3

    How are you determining QFile is stopping at the char( 0 )? If you're using a (const) char[], a std::string or QString any character after 0 won't be displayed even if set, as 0 is the string terminator. Are you sure the data is not loaded or could it be is is simply not displayed?

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

    1 Reply Last reply
    0
    • VRoninV VRonin

      Sure it's possible, can you show us the code you are using to read the file?

      ManiRonM Offline
      ManiRonM Offline
      ManiRon
      wrote on last edited by
      #4

      @VRonin

         file.read(checksum,32); // Actually first 32 byte are checksum and the remaining are data
          qDebug("Data %s",checksum);
          file.seek(33);
          size_of_file = file.size() - 33;
          qDebug("FILESIZE %d",size_of_file);
              fileData = file.read(size_of_file);
              qDebug("FILEDATA %s",fileData.data());
          qDebug("FILEDATA1 %s",fileData.data());
          FileHashData= QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
          qDebug() << FileHashData.toHex();
      
      JonBJ 1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #5

        All the types are missing there. What type is fileData? ho do you open file ?

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        ManiRonM 1 Reply Last reply
        0
        • ManiRonM ManiRon

          @VRonin

             file.read(checksum,32); // Actually first 32 byte are checksum and the remaining are data
              qDebug("Data %s",checksum);
              file.seek(33);
              size_of_file = file.size() - 33;
              qDebug("FILESIZE %d",size_of_file);
                  fileData = file.read(size_of_file);
                  qDebug("FILEDATA %s",fileData.data());
              qDebug("FILEDATA1 %s",fileData.data());
              FileHashData= QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
              qDebug() << FileHashData.toHex();
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #6

          @ManiRon said in How to make QFile read the whole line:

             file.read(checksum,32); // Actually first 32 byte are checksum and the remaining are data
              qDebug("Data %s",checksum);
              file.seek(33);
              size_of_file = file.size() - 33;
          

          If your checksum is 32 bytes it occupies bytes #0 to #31 inclusive (like an array). You then seek to 33. Just to verify, that means you are "skipping" 1 byte at offset 32 in the file. Is that really what you intend?

          ManiRonM 1 Reply Last reply
          0
          • JonBJ JonB

            @ManiRon said in How to make QFile read the whole line:

               file.read(checksum,32); // Actually first 32 byte are checksum and the remaining are data
                qDebug("Data %s",checksum);
                file.seek(33);
                size_of_file = file.size() - 33;
            

            If your checksum is 32 bytes it occupies bytes #0 to #31 inclusive (like an array). You then seek to 33. Just to verify, that means you are "skipping" 1 byte at offset 32 in the file. Is that really what you intend?

            ManiRonM Offline
            ManiRonM Offline
            ManiRon
            wrote on last edited by ManiRon
            #7

            @JonB 0_1542276715976_Untitled.jpg

            its a single line data i have taken sreenshot like this . After the data 61 the next starts as d2.

            As per the above image the file read reads till A.,?E>t1?dWQA.,DE_??n1?d7be1526df618c0c4d1bab3959adb65ada9f20bd969493e532a7861d29bc6e698

            JonBJ ManiRonM 2 Replies Last reply
            0
            • ManiRonM ManiRon

              @JonB 0_1542276715976_Untitled.jpg

              its a single line data i have taken sreenshot like this . After the data 61 the next starts as d2.

              As per the above image the file read reads till A.,?E>t1?dWQA.,DE_??n1?d7be1526df618c0c4d1bab3959adb65ada9f20bd969493e532a7861d29bc6e698

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #8

              @ManiRon
              I have no idea how that answers my question. Only you know how byte #32 is or is not used in the file. It's up to you.

              Meanwhile, you should be answering @VRonin's

              All the types are missing there. What type is fileData? ho do you open file ?

              1 Reply Last reply
              0
              • VRoninV VRonin

                All the types are missing there. What type is fileData? ho do you open file ?

                ManiRonM Offline
                ManiRonM Offline
                ManiRon
                wrote on last edited by ManiRon
                #9

                @VRonin int cRetVal = 0;

                  int cRetVal = 0;
                QByteArray FileHashData, fileData;
                char checksum[32];
                qint64 size_of_file;
                
                strUserName = ui->le_Username->text();
                m_usrname = ui->le_Username->text();
                
                
                char carrUsrPwdHash[65] = {"\0"};
                QString UsrPwdHash;
                
                QFile file("userpass");
                
                if (file.open(QIODevice::ReadOnly))
                {
                    file.read(checksum,32);
                    qDebug() << checksum;
                    file.seek(33);
                    size_of_file = file.size() - 33;
                    fileData = file.read(size_of_file);
                
                    qDebug("FILEDATA %s",fileData.data());
                    FileHashData= QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
                    qDebug() << FileHashData.toHex();
                }
                
                1 Reply Last reply
                0
                • ManiRonM ManiRon

                  @JonB 0_1542276715976_Untitled.jpg

                  its a single line data i have taken sreenshot like this . After the data 61 the next starts as d2.

                  As per the above image the file read reads till A.,?E>t1?dWQA.,DE_??n1?d7be1526df618c0c4d1bab3959adb65ada9f20bd969493e532a7861d29bc6e698

                  ManiRonM Offline
                  ManiRonM Offline
                  ManiRon
                  wrote on last edited by
                  #10

                  @ManiRon
                  in that image b5afa24a9fa264a178f5ae63c7eff6cf this is my checksum after that a Nul character is there after which my actual data starts

                  JonBJ 1 Reply Last reply
                  0
                  • ManiRonM ManiRon

                    @ManiRon
                    in that image b5afa24a9fa264a178f5ae63c7eff6cf this is my checksum after that a Nul character is there after which my actual data starts

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #11

                    @ManiRon
                    OK, in that case it sounds right if it is followed by a "Nul". Personally I would put a comment into your code then to explain that is why you are seeking to byte #33.

                    I don't see anything which would stop at a NUL in your code. What makes you think it is doing so? Are you just relying on the output of qDebug("FILEDATA %s",fileData.data()); and thinking that shows it has not read past a NUL, that may be an artefact of how qDebug() displays the data? Try debugging out the length of the data?

                    ManiRonM 2 Replies Last reply
                    1
                    • JonBJ JonB

                      @ManiRon
                      OK, in that case it sounds right if it is followed by a "Nul". Personally I would put a comment into your code then to explain that is why you are seeking to byte #33.

                      I don't see anything which would stop at a NUL in your code. What makes you think it is doing so? Are you just relying on the output of qDebug("FILEDATA %s",fileData.data()); and thinking that shows it has not read past a NUL, that may be an artefact of how qDebug() displays the data? Try debugging out the length of the data?

                      ManiRonM Offline
                      ManiRonM Offline
                      ManiRon
                      wrote on last edited by ManiRon
                      #12

                      @JonB Actually my requirement is to calculate checksum for the data and store it in the same file where the data i am writing to . So i planned to write the checksum at the begining and followed by the data.

                      Now when i start the application i will validate the file by reading the checksum stored in file and compare it with the remaining data in file by calculating checksum for them .

                      Note the stored checksum is calculated using the data and not the whole file

                      1 Reply Last reply
                      0
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #13

                        could you replace fileData = file.read(size_of_file); with :

                        fileData.resize(size_of_file);
                        const auto actualRead = file.read(fileData.data(),size_of_file);
                        Q_ASSERT(actualRead==size_of_file);
                        

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        ManiRonM 2 Replies Last reply
                        0
                        • VRoninV VRonin

                          could you replace fileData = file.read(size_of_file); with :

                          fileData.resize(size_of_file);
                          const auto actualRead = file.read(fileData.data(),size_of_file);
                          Q_ASSERT(actualRead==size_of_file);
                          
                          ManiRonM Offline
                          ManiRonM Offline
                          ManiRon
                          wrote on last edited by
                          #14

                          @VRonin said in How to make QFile read the whole line:

                          actualRead

                          1_1542281674674_Untitled2.jpg 0_1542281674660_Untitled1.jpg

                          I tried sir but i am getting this kind of error

                          jsulmJ 1 Reply Last reply
                          0
                          • ManiRonM ManiRon

                            @VRonin said in How to make QFile read the whole line:

                            actualRead

                            1_1542281674674_Untitled2.jpg 0_1542281674660_Untitled1.jpg

                            I tried sir but i am getting this kind of error

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

                            @ManiRon It's not hard to fix:

                            const qint64 actualRead = file.read(fileData.data(),size_of_file);
                            

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

                            ManiRonM 1 Reply Last reply
                            1
                            • jsulmJ jsulm

                              @ManiRon It's not hard to fix:

                              const qint64 actualRead = file.read(fileData.data(),size_of_file);
                              
                              ManiRonM Offline
                              ManiRonM Offline
                              ManiRon
                              wrote on last edited by
                              #16

                              @jsulm said in How to make QFile read the whole line:

                              const qint64 actualRead

                              error resolved

                              1 Reply Last reply
                              0
                              • VRoninV VRonin

                                could you replace fileData = file.read(size_of_file); with :

                                fileData.resize(size_of_file);
                                const auto actualRead = file.read(fileData.data(),size_of_file);
                                Q_ASSERT(actualRead==size_of_file);
                                
                                ManiRonM Offline
                                ManiRonM Offline
                                ManiRon
                                wrote on last edited by
                                #17

                                @VRonin but thsi doest read the full data

                                jsulmJ 1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @ManiRon
                                  OK, in that case it sounds right if it is followed by a "Nul". Personally I would put a comment into your code then to explain that is why you are seeking to byte #33.

                                  I don't see anything which would stop at a NUL in your code. What makes you think it is doing so? Are you just relying on the output of qDebug("FILEDATA %s",fileData.data()); and thinking that shows it has not read past a NUL, that may be an artefact of how qDebug() displays the data? Try debugging out the length of the data?

                                  ManiRonM Offline
                                  ManiRonM Offline
                                  ManiRon
                                  wrote on last edited by ManiRon
                                  #18

                                  @JonB

                                  No sir i checked the read by removing the NUL from the data in the file and i was able to see that it read the whole data and qDebug() was displaying the full data

                                  This is the whole data when i removed the NUL : " WQA.,?E>t1?dWQA.,DE_??n1?d7be1526df618c0c4d1bab3959adb65ada9f20bd969493e532a7861d29bc6e698{TB.)?V1??n1?dS[H21?z7_??n1?d340819615bcd7cb87459cac37070456eb0af53716c1f0064c4771857728d3777 "

                                  ManiRonM 1 Reply Last reply
                                  0
                                  • ManiRonM ManiRon

                                    @JonB

                                    No sir i checked the read by removing the NUL from the data in the file and i was able to see that it read the whole data and qDebug() was displaying the full data

                                    This is the whole data when i removed the NUL : " WQA.,?E>t1?dWQA.,DE_??n1?d7be1526df618c0c4d1bab3959adb65ada9f20bd969493e532a7861d29bc6e698{TB.)?V1??n1?dS[H21?z7_??n1?d340819615bcd7cb87459cac37070456eb0af53716c1f0064c4771857728d3777 "

                                    ManiRonM Offline
                                    ManiRonM Offline
                                    ManiRon
                                    wrote on last edited by
                                    #19

                                    @ManiRon

                                    But when the data conatins NUL the data will be " WQA.,?E>t1?dWQA.,DE_??n1?d7be1526df618c0c4d1bab3959adb65ada9f20bd969493e532a7861d29bc6e698 "

                                    1 Reply Last reply
                                    0
                                    • ManiRonM ManiRon

                                      @VRonin but thsi doest read the full data

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

                                      @ManiRon said in How to make QFile read the whole line:

                                      but thsi doest read the full data

                                      What is the output of

                                      qDebug() << actualRead << size_of_file;
                                      

                                      ?

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

                                      ManiRonM 1 Reply Last reply
                                      0
                                      • jsulmJ jsulm

                                        @ManiRon said in How to make QFile read the whole line:

                                        but thsi doest read the full data

                                        What is the output of

                                        qDebug() << actualRead << size_of_file;
                                        

                                        ?

                                        ManiRonM Offline
                                        ManiRonM Offline
                                        ManiRon
                                        wrote on last edited by ManiRon
                                        #21

                                        @jsulm said in How to make QFile read the whole line:

                                        size_of_file

                                        Size = 194 .

                                        I thought it was qdebug problem so i wrote the data read from the file to another file then also it remains the same as the data shown in the debug

                                        ManiRonM 1 Reply Last reply
                                        0
                                        • VRoninV Offline
                                          VRoninV Offline
                                          VRonin
                                          wrote on last edited by
                                          #22

                                          What is the value of actualRead?

                                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                          ~Napoleon Bonaparte

                                          On a crusade to banish setIndexWidget() from the holy land of Qt

                                          ManiRonM 1 Reply Last reply
                                          1

                                          • Login

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