Introduction Files can be written to their current position or from a specified position, as discussed here:
Write() with relative/current position There are four RFile::Write overload functions relative/current position that allow you to specify a position greater than 2GB–1 by using the TInt64 parameter.
RFile64::Write(const TDesC8& aDes)
RFile64::Write(const TDesC8& aDes,
- TRequestStatus& aStatus)
RFile64::Write(const TDesC8& aDes, TInt
- aLength)
RFile64::Write(const TDesC8& aDes, TInt aLength,
- TRequestStatus& aStatus).
Use RFile64 instead of RFile.
// RFile file;
-RFile64 file;
Use TInt64 variables in place of TInt variables where they may represent the number of bytes written.
// Tint bytesWritten = 0;
-Tint64 bytesWritten = 0;
Write with relative/current position 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)
- {
- // We will only get here if a real error condition occurs.
- 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() with absolute position There are four RFile::Write overload functions with absolute position that allow you to specify a position greater than 2GB–1 by using the TInt64 parameter.
RFile64::Write(TInt64 aPos, const TDesC8&
- aDes)
RFile64::Write(TInt64 aPos, const TDesC8& aDes,
- TRequestStatus& aStatus)
RFile64::Write(TInt64 aPos, const TDesC8& aDes, TInt
- aLength)
RFile64::Write(TInt64 aPos, const TDesC8& aDes, TInt
- aLength, TRequestStatus& aStatus).
Use a TInt64 instead of a TInt for the write position.
// TInt writePosition = 0;
- TInt64 writePosition = 0;
Replace RFile with RFile64.
// RFile file;
- RFile64 file;
Write with absolute position 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)
- {
- // We will only get here for upon encountering a real error condition.
- 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)
- {
- // We will only get here upon encountering a real error condition.
- return r;
- }
- writePosition += writeBuf.Length(); // Must use a TInt64
- }