Writing to Large Files

How to write to large files.

Write() from relative/current position

There are four RFile::Write() overloaded functions relative/current position that allow you to specify a position greater than 2GB–1 by using the TInt64 parameter.

The migration procedure is as follows:

  1. Use RFile64 instead of RFile.

    // RFile file;
    RFile64 file;
  2. Use TInt64 variables in place of the TInt variables representing the number of bytes written.

    // Tint bytesWritten = 0;
    Tint64 bytesWritten = 0;

Example

The example below shows an application writing to locations greater than 2GB–1. The RFile class has been replaced with RFile64 and the TInt variable that represents the number of bytes written has been changed to TInt64.

    // RFile file;
    RFile64 file;
    TInt err = file.Open(TheFs, _L(“BIGFILE.BIG”), EFileWrite);
    if(err != KErrNone)
        {
        // unrelated error
        return err;
        }

    // Tint bytesWritten = 0;
    Tint64 bytesWritten = 0;
    TBuf8<1024> writeBuf;
    ...

    FOREVER
        {
        // Fill buffer with input data
        r = prepareBuffer(writeBuf);
        if(KErrNone != r)
            {
            // No data to write!
            break;
            }
        
        r = file.Write(writeBuf);
        if(r != KErrNone)
            {
            // Error handling
            }
        bytesWritten += writeBuf.Length();
        }

Write() from absolute position

There are four RFile::Write() overloaded functions that allow you to specify an absolute position greater than 2GB–1 by using the TInt64 parameter.

The migration procedure is as follows:

  1. Use a TInt64 instead of a TInt for the write position.

    // TInt writePosition = 0;
        TInt64 writePosition = 0;
  2. Replace RFile with RFile64.

    // RFile file;
        RFile64 file;

Example

To write to locations beyond 2GB–1 the file must be opened for 64-bit access and TInt variables should be replaced with TInt64.

    // RFile file;
    RFile64 file;
    TInt err = file.Open(TheFs, _L(“BIGFILE.BIG”), EFileWrite);
    if(err != KErrNone)
     {
        // unrelated error
        return err;
     }

    // TInt writePosition = 0;
    TInt64 writePosition = 0;    // Use a TInt64 to prevent wrap-around
    TBuf8<1024> writeBuf;
    ...
    FOREVER
        {
        // Fill buffer with input data
        r = prepareBuffer(writeBuf);
        if(KErrNone != r)
            {
            // No data to write!
            break;
            }
        
        r = file.Write(writePosition, writeBuf);
        if(r != KErrNone)
            {
            // unrelated error
            return r;
            }
        writePosition += writeBuf.Length(); // Must use a TInt64
        }