Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. About the missing byte when using Qfile read a binary file
Qt 6.12 LTS is out! See what's new in the release blog

About the missing byte when using Qfile read a binary file

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 3.0k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I am using QFile to read a .cli file(which is used for 3D printing and),it is stored in binary order.(The byteOrder is least significant)

    Something wrong when I try to analyse the data,I used QFile getChar() function to get each data,but it missed a byte(0x0d, I know it is carriage return in ASCII but I am doing binary file reading so it should do nothing with ASCII) when I checked the result.

    Following is what I see in UltraEdit,it is clear that 0120h starts with C6-0A-0D-2B……However when I try to use QDebug to output each characteristic,the 0D disappeares.
    alt text

    To make sure whether it's due to getChar(),I tried QDataStream's readRawData() and QFile's read() ,but they also didn't work.

    I wrote a little program to test if 0x0d can't be read and the answer is yes.
    (I checked each byte in binary file,all byte value could be found except 00xd,which in decimal is 13).
    alt text

    Thank u all.

    raven-worxR 1 Reply Last reply
    0
    • ? A Former User

      I am using QFile to read a .cli file(which is used for 3D printing and),it is stored in binary order.(The byteOrder is least significant)

      Something wrong when I try to analyse the data,I used QFile getChar() function to get each data,but it missed a byte(0x0d, I know it is carriage return in ASCII but I am doing binary file reading so it should do nothing with ASCII) when I checked the result.

      Following is what I see in UltraEdit,it is clear that 0120h starts with C6-0A-0D-2B……However when I try to use QDebug to output each characteristic,the 0D disappeares.
      alt text

      To make sure whether it's due to getChar(),I tried QDataStream's readRawData() and QFile's read() ,but they also didn't work.

      I wrote a little program to test if 0x0d can't be read and the answer is yes.
      (I checked each byte in binary file,all byte value could be found except 00xd,which in decimal is 13).
      alt text

      Thank u all.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @MartinChan
      lets skip the guessing and show some code you are using.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Here it is and I omitted some details:

        //Read in binary file
        QFile file("e:/cli/6.cli");
        char *tmp=new char [1];
        
        if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
        {
        }else{
        while(!file.atEnd()){
                 //Read single byte each time
                    file.read(tmp,1);
                    int tmp_int=tmp[0]&(0xFF);/
        
                 //Output the result in hex 
                   QString key;
                    key =  QString("%1").arg(tmp_int,4,16,QLatin1Char('0'));
                    qDebug()<<key;
                   }
        }
        
        raven-worxR 1 Reply Last reply
        0
        • ? A Former User

          Here it is and I omitted some details:

          //Read in binary file
          QFile file("e:/cli/6.cli");
          char *tmp=new char [1];
          
          if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
          {
          }else{
          while(!file.atEnd()){
                   //Read single byte each time
                      file.read(tmp,1);
                      int tmp_int=tmp[0]&(0xFF);/
          
                   //Output the result in hex 
                     QString key;
                      key =  QString("%1").arg(tmp_int,4,16,QLatin1Char('0'));
                      qDebug()<<key;
                     }
          }
          
          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @MartinChan said in About the missing byte when using Qfile read a binary file:

          remove QIODevice::Text flag from the file.open() call

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          5
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            It worked!Thx ~

            I read the open() doc and it says:

            • When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.

            So it is due to the Qfile think 0x0D is '\r' when I falsely used it.

            1 Reply Last reply
            2

            • Login

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