PDA

View Full Version : Reading binary file size in bytes and assigning it to uint16_t



anita.niharika@gmail.com
2nd February 2021, 19:27
Hi All,

In below piece of code, I am trying to read the size of a binary file in bytes and assign it to a variable of type uint16_t.



QFile f (fileName);
uint16_t readSize;

f.open (QIODevice::ReadOnly);
qDebug()<<"size of a binary file "<<fileName<< "is in bytes"<<f.size ()<<"\n"; ===> 131071 (expected value)

readSize = f.size ();
qDebug()<<"size of a binary file "<<readSize<<"\n";====> 65535 (Incorrect value)

/* updating file size to a struct variable of type uint16_t
req.Size = f.size ();====> 65535 (Incorrect value)



When I change readSize data type from int then


readSize = f.size ();
qDebug()<<"size of a binary file "<<readSize<<"\n";====> 131071


Is this because of data type uint16_t restricting? or size of the file exceeding the range of uint16_t?

ChristianEhrlicher
2nd February 2021, 21:06
Is this because of data type uint16_t restricting?

Yes - you can't represent 131071 with only 16bits.