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.
  • ManiRonM ManiRon

    @JonB

    the size it specifies is correct but the data displayed is till the NUL

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

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

    @JonB

    the size it specifies is correct but the data displayed is till the NUL

    But I said that was just how qDebug() displays data. Read up about how qDebug() displays strings/bytes, which may contain NUL.

    ManiRonM 1 Reply Last reply
    0
    • JonBJ JonB

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

      @JonB

      the size it specifies is correct but the data displayed is till the NUL

      But I said that was just how qDebug() displays data. Read up about how qDebug() displays strings/bytes, which may contain NUL.

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

      @JonB

      I have doubt in storing the data i store the data read in QBytearray

      JonBJ 1 Reply Last reply
      0
      • ManiRonM ManiRon

        @JonB

        I have doubt in storing the data i store the data read in QBytearray

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

        @ManiRon
        So read up how qDebug() displays a QByteArray (as well as a QString)!!
        Something even as old as https://www.qtcentre.org/threads/54236-qDebug-donsn-t-display-0x00?p=243053#post243053 will presumably do you.....

        ManiRonM 1 Reply Last reply
        2
        • JonBJ JonB

          @ManiRon
          So read up how qDebug() displays a QByteArray (as well as a QString)!!
          Something even as old as https://www.qtcentre.org/threads/54236-qDebug-donsn-t-display-0x00?p=243053#post243053 will presumably do you.....

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

          @JonB

          Yes sir i can understand that it will take NUL as a terminate . Is there any way that i can store the full data along with the NUL

          Christian EhrlicherC 1 Reply Last reply
          0
          • ManiRonM ManiRon

            @JonB

            Yes sir i can understand that it will take NUL as a terminate . Is there any way that i can store the full data along with the NUL

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #34

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

            Is there any way that i can store the full data along with the NUL

            It is stored, just qDebug() stops it's output when it sees NUL. Take a look at the link @JonB provided above.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            ManiRonM 1 Reply Last reply
            3
            • Christian EhrlicherC Christian Ehrlicher

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

              Is there any way that i can store the full data along with the NUL

              It is stored, just qDebug() stops it's output when it sees NUL. Take a look at the link @JonB provided above.

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

              @Christian-Ehrlicher sir i checked the data are read from the file fully but the problem is the way i store . I tried to store it in a QByteArray variable, a char array Variable, a QString Variable but all are terminating when the data has a NUL Inbetween, Now its not the problem of qDebug .

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #36

                You really should try to understand what we wrote. When you QFile::read() returns exactly the size of the file then all bytes are read and stored in the QByteArray. Just because qDebug() stops at NUL doesn't mean there is no data after that...

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                ManiRonM 1 Reply Last reply
                3
                • Christian EhrlicherC Christian Ehrlicher

                  You really should try to understand what we wrote. When you QFile::read() returns exactly the size of the file then all bytes are read and stored in the QByteArray. Just because qDebug() stops at NUL doesn't mean there is no data after that...

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

                  @Christian-Ehrlicher

                  ok sir, But i tried to write the data what ever i stored in a QBytearray to another file and the result was same . Thats why i concluded that QByteArray terminate at NUL and its mentioned
                  here
                  http://doc.qt.io/qt-5/qbytearray.html#details

                  J.HilkJ 1 Reply Last reply
                  0
                  • ManiRonM ManiRon

                    @Christian-Ehrlicher

                    ok sir, But i tried to write the data what ever i stored in a QBytearray to another file and the result was same . Thats why i concluded that QByteArray terminate at NUL and its mentioned
                    here
                    http://doc.qt.io/qt-5/qbytearray.html#details

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by J.Hilk
                    #38

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

                    @Christian-Ehrlicher

                    ok sir, But i tried to write the data what ever i stored in a QBytearray to another file and the result was same . Thats why i concluded that QByteArray terminate at NUL and its mentioned
                    here
                    http://doc.qt.io/qt-5/qbytearray.html#details

                    where exactly ?
                    because the docu clearly sates otherwise and, thats in the section you linked!

                    ->
                    0_1542352803222_1ca28d44-9917-4c44-89dc-0ae3b23ac891-image.png


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    ManiRonM 1 Reply Last reply
                    2
                    • J.HilkJ J.Hilk

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

                      @Christian-Ehrlicher

                      ok sir, But i tried to write the data what ever i stored in a QBytearray to another file and the result was same . Thats why i concluded that QByteArray terminate at NUL and its mentioned
                      here
                      http://doc.qt.io/qt-5/qbytearray.html#details

                      where exactly ?
                      because the docu clearly sates otherwise and, thats in the section you linked!

                      ->
                      0_1542352803222_1ca28d44-9917-4c44-89dc-0ae3b23ac891-image.png

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

                      @J.Hilk 0_1542353448743_Capture.JPG

                      In this line its mentioned

                      JonBJ 1 Reply Last reply
                      0
                      • ManiRonM ManiRon

                        @J.Hilk 0_1542353448743_Capture.JPG

                        In this line its mentioned

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

                        @ManiRon
                        What is mentioned?? Oh dear! If you read, that is talking about it is an extra '\0'/NUL byte terminator which QByteArray chooses to store at the end of whatever data you have asked it to store. It has nothing to do with any NUL byte you have in your actual data. So far as you/your code is concerned, simply 100% ignore this extra byte, you won't see it and it won't affect you.

                        Because your data is not a NUL terminated string, but rather arbitrary bytes of data, make sure anything you copy into to/out of a QByteArray always specifies the size/number of bytes you want copied, and does not use a call which says it terminates at a NUL/treats it like a string.

                        For example, say you have your data in a char[], or similar. You would want to use http://doc.qt.io/qt-5/qbytearray.html#QByteArray-1

                        QByteArray::QByteArray(const char *data, int size = -1)

                        However, you must not fail to pass in size and allow that to take its default value of -1. If you do that, it will only copy up to wherever the first NUL byte happens to sit in your data buffer. That is not what you want (assuming you want to copy everything). In your case you must explicitly pass the desired number of bytes to copy as size, then it will copy all your embedded NUL bytes too.

                        And please wherever you do use qDebug() to see what's going on, follow the link I gave you https://www.qtcentre.org/threads/54236-qDebug-donsn-t-display-0x00?p=243053#post243053 where it suggests you use:

                        qDebug() << yourQByteArrayVariable.toHex();
                        

                        See that .toHex() at the end?

                        1 Reply Last reply
                        4
                        • J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #41

                          @JonB I agree with your points, but did you see the op apparently calls toHex on his byteArray

                          https://forum.qt.io/topic/96627/how-to-make-qfile-read-the-whole-line/9

                          but maybe at the wrong place, it's after QCryptographicHash is done with it.


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          JonBJ ManiRonM 2 Replies Last reply
                          0
                          • J.HilkJ J.Hilk

                            @JonB I agree with your points, but did you see the op apparently calls toHex on his byteArray

                            https://forum.qt.io/topic/96627/how-to-make-qfile-read-the-whole-line/9

                            but maybe at the wrong place, it's after QCryptographicHash is done with it.

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

                            @J.Hilk , @ManiRon
                            so the OP needs to

                            • Display the length/hex representation of fileData.data() before calling QCryptographicHash::hash.
                            • Display the length/hex representation of FileHashData after calling QCryptographicHash::hash.
                            • How do we know the output from
                                FileHashData= QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
                                qDebug() << FileHashData.toHex();
                            

                            is actually "wrong"? I haven't seen that. Is it shorter than the input? Do we have any reason to presume that QCryptographicHash::hash(fileData, QCryptographicHash::Md5) output must be shorter/same length/longer than its input?

                            Hang on: he's outputting FileHashData.toHex();. But don't you have to use http://doc.qt.io/qt-5/qcryptographichash.html#result to get the result, no? I've never used any of these functions, so how would I know, but I make the effort to read the docs. Can the OP do that himself?

                            1 Reply Last reply
                            0
                            • J.HilkJ J.Hilk

                              @JonB I agree with your points, but did you see the op apparently calls toHex on his byteArray

                              https://forum.qt.io/topic/96627/how-to-make-qfile-read-the-whole-line/9

                              but maybe at the wrong place, it's after QCryptographicHash is done with it.

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

                              @J.Hilk

                              Sir, Actually i am in a confusion . Now i checked in different mannerwith the QByteArray but it had all the read data . But in qDebug it showed the data till the NUL is accomplished . I tried printing the data char by char till the whole size of file and i was able to see that the QByteArray variable had the whole data . So i think it maybe the qDebug problem as you guys mentioned . But my another doubt is if the QByteArray has all the data when i wrote the same data in file why it didnt write and why it wrote till first NUL?

                              JonBJ JKSHJ 2 Replies Last reply
                              0
                              • ManiRonM ManiRon

                                @J.Hilk

                                Sir, Actually i am in a confusion . Now i checked in different mannerwith the QByteArray but it had all the read data . But in qDebug it showed the data till the NUL is accomplished . I tried printing the data char by char till the whole size of file and i was able to see that the QByteArray variable had the whole data . So i think it maybe the qDebug problem as you guys mentioned . But my another doubt is if the QByteArray has all the data when i wrote the same data in file why it didnt write and why it wrote till first NUL?

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

                                @ManiRon

                                But in qDebug it showed the data till the NUL is accomplished . I tried printing the data char by char till the whole size of file and i was able to see that the QByteArray variable had the whole data .

                                I give up. How many times can one say that qDebug() << QByteArray will stop at NUL so not to use it?

                                ManiRonM 1 Reply Last reply
                                2
                                • JonBJ JonB

                                  @ManiRon

                                  But in qDebug it showed the data till the NUL is accomplished . I tried printing the data char by char till the whole size of file and i was able to see that the QByteArray variable had the whole data .

                                  I give up. How many times can one say that qDebug() << QByteArray will stop at NUL so not to use it?

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

                                  @JonB

                                  oh ok sir. But if that is the case it should stop when i try to print it using a char by char right? Just a doubt sir?

                                  JonBJ 1 Reply Last reply
                                  0
                                  • ManiRonM ManiRon

                                    @JonB

                                    oh ok sir. But if that is the case it should stop when i try to print it using a char by char right? Just a doubt sir?

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

                                    @ManiRon
                                    I'm sorry but I cannot find anyway to explain and answer your questions any longer. Maybe someone else can explain better than I to you.

                                    I would however suggest that you/someone look at what I said above about http://doc.qt.io/qt-5/qcryptographichash.html#result and see whether you need to be using that to get your hash result.

                                    ManiRonM 1 Reply Last reply
                                    0
                                    • JonBJ JonB

                                      @ManiRon
                                      I'm sorry but I cannot find anyway to explain and answer your questions any longer. Maybe someone else can explain better than I to you.

                                      I would however suggest that you/someone look at what I said above about http://doc.qt.io/qt-5/qcryptographichash.html#result and see whether you need to be using that to get your hash result.

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

                                      @JonB no sir i can understand what u are trying to explain . dont mistake me

                                      1 Reply Last reply
                                      0
                                      • ManiRonM ManiRon

                                        @J.Hilk

                                        Sir, Actually i am in a confusion . Now i checked in different mannerwith the QByteArray but it had all the read data . But in qDebug it showed the data till the NUL is accomplished . I tried printing the data char by char till the whole size of file and i was able to see that the QByteArray variable had the whole data . So i think it maybe the qDebug problem as you guys mentioned . But my another doubt is if the QByteArray has all the data when i wrote the same data in file why it didnt write and why it wrote till first NUL?

                                        JKSHJ Offline
                                        JKSHJ Offline
                                        JKSH
                                        Moderators
                                        wrote on last edited by
                                        #48

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

                                        if the QByteArray has all the data when i wrote the same data in file why it didnt write and why it wrote till first NUL?

                                        Show us how you wrote the data to the file.

                                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                        1 Reply Last reply
                                        0

                                        • Login

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