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 can I read strings from a binary file?
Forum Updated to NodeBB v4.3 + New Features

How can I read strings from a binary file?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.4k 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.
  • GuerrianG Offline
    GuerrianG Offline
    Guerrian
    wrote on last edited by
    #1

    This is what I have so far:

    QFile file;
    QString str1, str2;
    file.setFileName(file_name);
    if (file.open(QIODevice::ReadOnly))
    {
        QDataStream data(&file);
        data >> str1;
        data >> str2;
    }
    file.close();
    

    Will this work or do I have to store the string sizes in the file?

    A 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What do you use to write that file ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • GuerrianG Guerrian

        This is what I have so far:

        QFile file;
        QString str1, str2;
        file.setFileName(file_name);
        if (file.open(QIODevice::ReadOnly))
        {
            QDataStream data(&file);
            data >> str1;
            data >> str2;
        }
        file.close();
        

        Will this work or do I have to store the string sizes in the file?

        A Offline
        A Offline
        ambershark
        wrote on last edited by ambershark
        #3

        @Guerrian Assuming you wrote it with a QDataStream/QFile as binary then no you will not need the lengths of each string.

        Edit: as a side note you could have just compiled your example and tested it faster than writing this question. :D

        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

        GuerrianG 1 Reply Last reply
        4
        • A ambershark

          @Guerrian Assuming you wrote it with a QDataStream/QFile as binary then no you will not need the lengths of each string.

          Edit: as a side note you could have just compiled your example and tested it faster than writing this question. :D

          GuerrianG Offline
          GuerrianG Offline
          Guerrian
          wrote on last edited by Guerrian
          #4

          @ambershark Not really, because I would have had two versions of my program each with a load and a save function... too much editing.

          A 1 Reply Last reply
          0
          • GuerrianG Guerrian

            @ambershark Not really, because I would have had two versions of my program each with a load and a save function... too much editing.

            A Offline
            A Offline
            ambershark
            wrote on last edited by
            #5

            @Guerrian Yea but I literally took your code, added it to a main(), wrote a quick file using QFile as binary, and then read it using your code. Took me < 2 minutes. You didn't have to edit your actual code, you can just create a test app. Even if you were super slow and it took you 20 min to test it, it's still faster than asking a forum. ;)

            My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

            GuerrianG 2 Replies Last reply
            3
            • A ambershark

              @Guerrian Yea but I literally took your code, added it to a main(), wrote a quick file using QFile as binary, and then read it using your code. Took me < 2 minutes. You didn't have to edit your actual code, you can just create a test app. Even if you were super slow and it took you 20 min to test it, it's still faster than asking a forum. ;)

              GuerrianG Offline
              GuerrianG Offline
              Guerrian
              wrote on last edited by Guerrian
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • A ambershark

                @Guerrian Yea but I literally took your code, added it to a main(), wrote a quick file using QFile as binary, and then read it using your code. Took me < 2 minutes. You didn't have to edit your actual code, you can just create a test app. Even if you were super slow and it took you 20 min to test it, it's still faster than asking a forum. ;)

                GuerrianG Offline
                GuerrianG Offline
                Guerrian
                wrote on last edited by Guerrian
                #7

                @ambershark I sussed this out, but it took me a lot longer than 20 minutes. I'm new to Qt and my program had a bug which I had to fix before getting this right. It works like you said. Thanks.

                A 1 Reply Last reply
                2
                • GuerrianG Guerrian

                  @ambershark I sussed this out, but it took me a lot longer than 20 minutes. I'm new to Qt and my program had a bug which I had to fix before getting this right. It works like you said. Thanks.

                  A Offline
                  A Offline
                  ambershark
                  wrote on last edited by
                  #8

                  @Guerrian Yea sorry I didn't factor learning time or bugs into my times. :) Unfortunately while learning everything takes so much longer. But the experience is what makes the memory in the future.

                  My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                  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