Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Copying QFile contents to another QFile, what's the fastest way?
Qt 6.12 LTS is out! See what's new in the release blog

Copying QFile contents to another QFile, what's the fastest way?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.0k 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.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on last edited by
    #1

    I need to copy a QFile to another QFile in chunks, so I can't use QFile::copy. Here's the most primitive implementation:
    @bool CFile::copyChunk(int64_t chunkSize, const QString &destFolder)
    {
    if (!_thisFile.isOpen())
    {
    // Initializing - opening files
    _thisFile.setFileName(_absoluteFilePath);
    if (!_thisFile.open(QFile::ReadOnly))
    return false;

        _destFile.setFileName(destFolder + _thisFileName);
        if (!_destFile.open(QFile::WriteOnly))
            return false;
    }
    
    if (chunkSize < (_thisFile.size() - _thisFile.pos()))
    {
        QByteArray data (chunkSize, 0);
        _thisFile.read(data.data(), chunkSize);
        return _destFile.write(data) == chunkSize;
    }
    

    }
    @
    It's not clear from this fragment, but I only intend to copy a binary file as a whole into another location, just in chunks so I can provide progress callbacks and cancellation facility for large files.

    Another idea is to use memory mapping. Should I? If so, then should I only map source file and still use _destFile.write, or should I map both and use memcpy?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      You know about the "static copy method?":http://qt-project.org/doc/qt-5.0/qtcore/qfile.html#copy-2
      Probably it will be hard to get faster, would be my assumption.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Violet Giraffe
        wrote on last edited by
        #3

        Like I've meantioned, I can't use that method. No doubt it's the fastest but it's blocking so I can't cancel a long copy if I use it.

        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
        • Unsolved