Results 1 to 4 of 4

Thread: QFile read(to, 65535) returns -1 on Windows 7/VS2010, works on linux

  1. #1
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QFile read(to, 65535) returns -1 on Windows 7/VS2010, works on linux

    Very simple:
    1) pBuffer is char array (65535) long
    2) file on disk is 8589 bytes
    3) read returns -1 so qFatal in the tiny example gets executed - but why? If another value is put there, 20000 for example works (Size == 8589), 50000 returns -1

    Qt Code:
    1. QFile File("myfile");
    2. if (File.open(QIODevice::ReadOnly)) {
    3. qDebug() << "Fail";
    4. return false;
    5. }
    6. qint64 Size = read(pBuffer, 65535); // The maximum number (looks like a hard number) what works is 38002
    7. if (Size == -1) {
    8. qFatal("Why?");
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by sandor; 8th May 2011 at 14:31.

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: QFile read(to, 65535) returns -1 on Windows 7/VS2010, works on linux

    "read returns -1 so qFatal in the tiny example gets executed - but why"

    You are executing a function "read" from whatever source, but not File.read() that you probably want to...

  3. #3
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFile read(to, 65535) returns -1 on Windows 7/VS2010, works on linux

    Okay, the code example was a little bit edited before putting here so, it is correctly:

    Qt Code:
    1. QFile File("myfile");
    2. if (File.open(QIODevice::ReadOnly)) {
    3. qDebug() << "Fail";
    4. return false;
    5. }
    6. qint64 Size = File.read(pBuffer, 65535); // The maximum number (looks like a hard number) what works is 38002
    7. if (Size == -1) {
    8. qFatal("Why?");
    9. }
    To copy to clipboard, switch view to plain text mode 

    The question is the same, why does it fail on Windows 7?

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFile read(to, 65535) returns -1 on Windows 7/VS2010, works on linux

    Even if you use the Windows API ReadFile function, it performs differently on XP vs W7.

    So the only advice is to not try and read more bytes than are available to be read (eg. get the file size first and only read that many bytes)

Similar Threads

  1. Replies: 1
    Last Post: 7th April 2010, 18:13
  2. Works well on Windows but fails on Linux
    By utkuaydin in forum Qt Programming
    Replies: 3
    Last Post: 11th January 2010, 13:22
  3. QFile::read failing on XP SP3
    By drescherjm in forum Qt Programming
    Replies: 7
    Last Post: 15th April 2009, 21:59
  4. QPalette works differently on windows and linux
    By babu198649 in forum Newbie
    Replies: 3
    Last Post: 6th March 2008, 07:27
  5. Project won't compile under Windows (works under Linux)
    By philski in forum Qt Programming
    Replies: 7
    Last Post: 14th September 2006, 15:29

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
  •  
Qt is a trademark of The Qt Company.