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. Reading file byte by byte
QtWS25 Last Chance

Reading file byte by byte

Scheduled Pinned Locked Moved Solved General and Desktop
readhex format
9 Posts 4 Posters 16.3k Views
  • 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.
  • J Offline
    J Offline
    jelicicm
    wrote on last edited by jelicicm
    #1

    Hi,

    Is it possible to read a file (extension unknown because it may vary), and simply read it byte by byte or whatever?
    I would like to be able to open a file (like you can with HexEditors) and save those values somewhere (in a string variable or so). I understand that those values won't mean anything to me when I look at them but I would be able to copy that file (write contents from string variable to a new file), and then save that new file.

    Is this possible, and how?

    Thank you.

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      yoavmil
      wrote on last edited by
      #2

      you could just copy the file using QFile::copy.
      you can also read a file to a QByteArray with readAll function, and save it to another file with write function

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jelicicm
        wrote on last edited by
        #3

        Thanks for the quick reply!

        This option occurred to me and I've dropped the idea. Here is why>
        I don't know what kind of files my program will be using. It might be .txt or .xlsx or whatever.

        Now, if its txt your suggestion works perfectly, but if its something Qt doesn't know how to read (Excel for example), then it fails.
        However, HxD (hex editor I have installed) reads the xlsx file and prints out hexadecimal values of it. I of course can't use it that way, but I still have some kind of read of that file (to store in a database for example, or copy it, or something). So I was hoping Qt can do a thing similar to that.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rondog
          wrote on last edited by
          #4

          You can read byte by byte if you want. Use the member function 'read(char*, qint64)'.

          QFile file;
          char file_data;
          file.open(QIODevice::ReadOnly);
          while(!file.atEnd())
          {
            // return value from 'file.read' should always be sizeof(char).
            file.read(&file_data,sizeof(char));
          
            // do something with 'file_data'.
          }
          file.close();
          

          When you open the file don't include the option QIODevice::Text. With this flag set Qt will be looking for line terminators and trying to convert them. With binary files this scrambles the data.

          If you know the data will be formatted a specific way (an array of double precision numbers for example) you can use this to your advantage and read in blocks of 8 bytes (sizeof(double)).

          You can tell if the file is binary or text by taking a sample of the data and look for anything below ascii 0x20 that is not a formatting character (\r,\n,\t). Some text files (rare but not unheard of) are unicode so you should exclude 0x00 if deciding if the file is binary.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jelicicm
            wrote on last edited by
            #5
            QFile file("C:\\Users\\s\\Desktop\\bom1.xlsx");
                char file_data;
                QString string;
                if(!file.open(QFile::ReadOnly))
                {
                    qDebug() << " Could not open the file for reading";
                    return;
                }
            
                while(!file.atEnd())
                {
                  // return value from 'file.read' should always be sizeof(char).
                  file.read(&file_data,sizeof(char));
                  string.append(file_data);
                  // do something with 'file_data'.
            
                }
                file.close();
            
                QFile out("C:\\Users\\s\\Desktop\\bom2");
            
                if(!out.open(QFile::WriteOnly))
                {
                    qDebug() << " Could not open the file for writing";
                    return;
                }
                const char* upis = string.toStdString().c_str();
                out.write(upis);
            
                out.close();
            
            

            So I tried this and it doesn't work. It copied some values, but it stopped when if first occurred empty space.

            Look> Above is the line from copied file, and below it's original file
            Alt text

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

              Hi,

              No it didn't stop but you are storing your char in a QString which will treat \0 as a terminating char (which is normal) What you want to use in your case is a QByteArray.

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

              J 1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                No it didn't stop but you are storing your char in a QString which will treat \0 as a terminating char (which is normal) What you want to use in your case is a QByteArray.

                J Offline
                J Offline
                jelicicm
                wrote on last edited by
                #7

                @SGaist No. I tried this> I defined string variable as QByteArray(I know its confusing but its just for testing) and it also stops and NULL terminator.

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

                  I missed a part of your code:

                  """
                  const char* upis = string.toStdString().c_str(); << Why ?
                  out.write(upis);
                  """

                  You're taking the c-string representation of a temporary std::string, which you are lucky is still accessible. Then you call write with a const char * without giving it's size, so the first \0 will act as a string termination character.

                  QFile::write has an overload for QByteArray so just call out.write(string) (also, you shouldn't call a QByteArray string, it's even more confusing)

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

                  J 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    I missed a part of your code:

                    """
                    const char* upis = string.toStdString().c_str(); << Why ?
                    out.write(upis);
                    """

                    You're taking the c-string representation of a temporary std::string, which you are lucky is still accessible. Then you call write with a const char * without giving it's size, so the first \0 will act as a string termination character.

                    QFile::write has an overload for QByteArray so just call out.write(string) (also, you shouldn't call a QByteArray string, it's even more confusing)

                    J Offline
                    J Offline
                    jelicicm
                    wrote on last edited by
                    #9

                    @SGaist You're right. Now it works.

                    Thanks a lot!

                    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