Results 1 to 2 of 2

Thread: Reading binary file size in bytes and assigning it to uint16_t

  1. #1
    Join Date
    Jan 2021
    Posts
    20
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Reading binary file size in bytes and assigning it to uint16_t

    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.
    Qt Code:
    1. QFile f (fileName);
    2. uint16_t readSize;
    3.  
    4. f.open (QIODevice::ReadOnly);
    5. qDebug()<<"size of a binary file "<<fileName<< "is in bytes"<<f.size ()<<"\n"; ===> 131071 (expected value)
    6.  
    7. readSize = f.size ();
    8. qDebug()<<"size of a binary file "<<readSize<<"\n";====> 65535 (Incorrect value)
    9.  
    10. /* updating file size to a struct variable of type uint16_t
    11. req.Size = f.size ();====> 65535 (Incorrect value)
    To copy to clipboard, switch view to plain text mode 

    When I change readSize data type from int then
    Qt Code:
    1. readSize = f.size ();
    2. qDebug()<<"size of a binary file "<<readSize<<"\n";====> 131071
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    524
    Thanked 81 Times in 76 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading binary file size in bytes and assigning it to uint16_t

    Quote Originally Posted by anita.niharika@gmail.com View Post
    Is this because of data type uint16_t restricting?
    Yes - you can't represent 131071 with only 16bits.

  3. The following user says thank you to ChristianEhrlicher for this useful post:

    anita.niharika@gmail.com (10th February 2021)

Similar Threads

  1. Reading a binary file with QT
    By YuriyRusinov in forum Qt Programming
    Replies: 7
    Last Post: 21st December 2012, 08:14
  2. Replies: 4
    Last Post: 23rd October 2012, 09:40
  3. reading bytes out of a file
    By priceey in forum Qt Programming
    Replies: 7
    Last Post: 6th October 2009, 17:55
  4. Reading and writing bytes in file
    By DiamonDogX in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2009, 21:25
  5. reading 4-bytes integer from binary file
    By maka in forum Qt Programming
    Replies: 8
    Last Post: 12th May 2009, 06:57

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.