About the missing byte when using Qfile read a binary file



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


  • Moderators

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



  • 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;
               }
    }
    

  • Moderators

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

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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.