Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFile::read(char *data, qint64 maxSize) return value
Qt 6.11 is out! See what's new in the release blog

QFile::read(char *data, qint64 maxSize) return value

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 380 Views 1 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.
  • E Offline
    E Offline
    equeim
    wrote on last edited by
    #1

    When reading file chunk-by-chunk and QFile::read(char *data, qint64 maxSize) returns positive value that is less than maxSize, what exactly does this mean? It it safe to keep calling read() until it returns either -1 or 0 or does this mean that there was an error and I should abort reading?

    Documentation on virtual QIODevice::readData function that is used to implement QFile states that "When reimplementing this function it is important that this function reads all the required data before returning." which implies that return value that is anything other than maxSize (and possibly 0 for end-of-file?) is an error condition. However QIODevice::read documentation doesn't mention this.

    Can I rely on read() doing the job of reading maxSize of bytes (excepting errors and end of file) or should I implement this logic myself?

    mzimmersM 1 Reply Last reply
    0
    • E equeim

      When reading file chunk-by-chunk and QFile::read(char *data, qint64 maxSize) returns positive value that is less than maxSize, what exactly does this mean? It it safe to keep calling read() until it returns either -1 or 0 or does this mean that there was an error and I should abort reading?

      Documentation on virtual QIODevice::readData function that is used to implement QFile states that "When reimplementing this function it is important that this function reads all the required data before returning." which implies that return value that is anything other than maxSize (and possibly 0 for end-of-file?) is an error condition. However QIODevice::read documentation doesn't mention this.

      Can I rely on read() doing the job of reading maxSize of bytes (excepting errors and end of file) or should I implement this logic myself?

      mzimmersM Offline
      mzimmersM Offline
      mzimmers
      wrote on last edited by
      #2

      @equeim from the docs:

      Reads at most maxSize bytes from the device into data...
      (emphasis mine)

      So the answer to your question is "yes," read will read as much data as:

      1. is available
      2. is <= your maxSize value

      Note, however, that if this is a device that provides information in chunks (like the old spectrum analyzers), you may need to continue to read (with some timeout value set) until read() returns a 0.

      E 1 Reply Last reply
      2
      • mzimmersM mzimmers

        @equeim from the docs:

        Reads at most maxSize bytes from the device into data...
        (emphasis mine)

        So the answer to your question is "yes," read will read as much data as:

        1. is available
        2. is <= your maxSize value

        Note, however, that if this is a device that provides information in chunks (like the old spectrum analyzers), you may need to continue to read (with some timeout value set) until read() returns a 0.

        E Offline
        E Offline
        equeim
        wrote on last edited by
        #3

        @mzimmers Hmm. Usually it's a QFile constructed from local file path, but it also can be opened from a file descriptor returned from Android's ContentResolver.openAssetFileDescriptor() (which could be just about anything, although it's usually either a file or a pipe that's written to dynamically on the other end).

        1 Reply Last reply
        0

        • Login

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