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. What's the optimal way to read QFile into a `vector<char> buffer`?
Forum Update on Monday, May 27th 2025

What's the optimal way to read QFile into a `vector<char> buffer`?

Scheduled Pinned Locked Moved Unsolved General and Desktop
noobbasicqfile
2 Posts 2 Posters 547 Views
  • 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.
  • shenlebantongyingS Offline
    shenlebantongyingS Offline
    shenlebantongying
    wrote on last edited by
    #1

    We have an app that read a large chunk of data frequently from various files via QFile::readAll. The file's size is not fixed. We have to copy the returned QByteArray to the vector<char> buffer.

    What if I don't want this little overhead and want extreme fast file reading?

    Would QFileDevice::map then memcpy be faster?

    Should I use DataStream?

    Or just plain read?

    If plain read is preferred, then please criticize and tell me why the piece of code that i came up with maybe stupid :)

    Why should the chunk_size be 1024? I saw many different values based on public codes.

    Also Can I read the file's content directly into the vector<char>?

        QFile f("what.txt");
    
        std::vector<char> target;
        int chunk_size = 1024;
        QByteArray buffer;
    
        f.open(QIODevice::ReadOnly);
    
        while (!f.atEnd()) {
            buffer = f.read(chunk_size);
            target.insert(target.end(), buffer.cbegin(), buffer.cend());
        }
    
        f.close();
    

    Thanks for your help!

    Christian EhrlicherC 1 Reply Last reply
    0
    • shenlebantongyingS shenlebantongying

      We have an app that read a large chunk of data frequently from various files via QFile::readAll. The file's size is not fixed. We have to copy the returned QByteArray to the vector<char> buffer.

      What if I don't want this little overhead and want extreme fast file reading?

      Would QFileDevice::map then memcpy be faster?

      Should I use DataStream?

      Or just plain read?

      If plain read is preferred, then please criticize and tell me why the piece of code that i came up with maybe stupid :)

      Why should the chunk_size be 1024? I saw many different values based on public codes.

      Also Can I read the file's content directly into the vector<char>?

          QFile f("what.txt");
      
          std::vector<char> target;
          int chunk_size = 1024;
          QByteArray buffer;
      
          f.open(QIODevice::ReadOnly);
      
          while (!f.atEnd()) {
              buffer = f.read(chunk_size);
              target.insert(target.end(), buffer.cbegin(), buffer.cend());
          }
      
          f.close();
      

      Thanks for your help!

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Use QFile::read(char* qint64).

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      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