Symbian3/PDK/Source/GUID-D71D0091-5C4E-5B2F-B1B6-783F11AB5021.dita
author Dominic Pinkman <Dominic.Pinkman@Nokia.com>
Fri, 22 Jan 2010 18:26:19 +0000
changeset 1 25a17d01db0c
child 3 46218c8b8afa
permissions -rw-r--r--
Addition of the PDK content and example code for Documentation_content according to Feature bug 1607 and bug 1608

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License 
"Eclipse Public License v1.0" which accompanies this distribution, 
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
    Nokia Corporation - initial contribution.
Contributors: 
-->
<!DOCTYPE concept
  PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept xml:lang="en" id="GUID-D71D0091-5C4E-5B2F-B1B6-783F11AB5021"><title>Writing to Large Files </title><shortdesc>This tutorial shows how to write to large files. </shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody><section><title>Introduction</title> <p>Files can be written to their current position or from a specified position, as discussed here: </p> <ul><li id="GUID-C5F51AAB-BC73-5B63-BE8B-F7142376C834"><p> <xref href="GUID-E9C763D4-AFFF-5D98-B82A-AF390A1B56A2.dita#GUID-E9C763D4-AFFF-5D98-B82A-AF390A1B56A2/GUID-559A2122-D5EF-53D3-9FA7-44F11B25D739"> Write() with relative/current position</xref>  </p> </li> <li id="GUID-6C463908-6614-5D6E-94D5-52721E3F5759"><p> <xref href="GUID-E9C763D4-AFFF-5D98-B82A-AF390A1B56A2.dita#GUID-E9C763D4-AFFF-5D98-B82A-AF390A1B56A2/GUID-52200A55-E323-5C7D-99A2-86F61E4D7F32"> Write() with absolute position</xref>. </p> </li> </ul> </section> <section><title>Write() with relative/current position</title> <p>There are four <xref href="GUID-BE0804F6-4375-3C8A-8C83-968F510466E0.dita#GUID-BE0804F6-4375-3C8A-8C83-968F510466E0/GUID-7CA5896D-6FC2-32F4-96F8-864BDAC9861C"><apiname>RFile::Write</apiname></xref> overload functions relative/current position that allow you to specify a position greater than 2GB–1 by using the <xref href="GUID-AAE85115-A8AA-3F6C-BA8C-69F5A23B0D90.dita"><apiname>TInt64</apiname></xref> parameter. </p> <ul><li id="GUID-D5A71E59-F7FA-51D3-A85E-35D4E46D1A9F"><p> <xref href="GUID-83A415A0-F417-3CA5-BA10-5AEF119AB1F7.dita#GUID-83A415A0-F417-3CA5-BA10-5AEF119AB1F7/GUID-C7A290AB-3EA5-3F0E-A587-31E199F17855"><apiname>RFile64::Write(const TDesC8&amp; aDes)</apiname></xref>  </p> </li> <li id="GUID-9A023D6F-98B5-58D9-B0E2-38EAF3FC7AA6"><p> <xref href="GUID-83A415A0-F417-3CA5-BA10-5AEF119AB1F7.dita#GUID-83A415A0-F417-3CA5-BA10-5AEF119AB1F7/GUID-A9785313-9473-3CEE-A491-CFFDC3670F4D"><apiname>RFile64::Write(const TDesC8&amp; aDes,
                TRequestStatus&amp; aStatus)</apiname></xref>  </p> </li> <li id="GUID-8FFE6422-3957-57C8-BEAE-450D28FC70F6"><p> <xref href="GUID-83A415A0-F417-3CA5-BA10-5AEF119AB1F7.dita#GUID-83A415A0-F417-3CA5-BA10-5AEF119AB1F7/GUID-B8A1627D-48AB-38E4-9CFF-09447E20C6DA"><apiname>RFile64::Write(const TDesC8&amp; aDes, TInt
                aLength)</apiname></xref>  </p> </li> <li id="GUID-0F0AFF90-0635-51C9-9B35-83E87E1A3F0C"><p> <xref href="GUID-83A415A0-F417-3CA5-BA10-5AEF119AB1F7.dita#GUID-83A415A0-F417-3CA5-BA10-5AEF119AB1F7/GUID-B0892D23-126A-3A84-B1D3-559572120491"><apiname>RFile64::Write(const TDesC8&amp; aDes, TInt aLength,
                TRequestStatus&amp; aStatus)</apiname></xref>. </p> </li> </ul> <ol id="GUID-C64C5E8A-A481-5050-A415-ACE8CA0694D0"><li id="GUID-B61FB842-0C8D-5CB2-9BAC-FE033C216430"><p>Use <xref href="GUID-83A415A0-F417-3CA5-BA10-5AEF119AB1F7.dita"><apiname>RFile64</apiname></xref> instead of <xref href="GUID-BE0804F6-4375-3C8A-8C83-968F510466E0.dita"><apiname>RFile</apiname></xref>. </p> <p><codeblock id="GUID-6A0E7FAD-2602-50DB-A200-3A3FB2CE7C56" xml:space="preserve">// RFile file;
RFile64 file;</codeblock> </p> </li> <li id="GUID-7CC3BEFE-19F7-5FB1-BB99-152A4911BA69"><p>Use <xref href="GUID-AAE85115-A8AA-3F6C-BA8C-69F5A23B0D90.dita"><apiname>TInt64</apiname></xref> variables in place of <xref href="GUID-7A2A43EC-6125-3BFE-834B-23C37F7B40D5.dita"><apiname>TInt</apiname></xref> variables where they may represent the number of bytes written. </p> <p><codeblock id="GUID-34FC7C71-EA56-5779-8E62-0729998AF405" xml:space="preserve">// Tint bytesWritten = 0;
Tint64 bytesWritten = 0;</codeblock> </p> </li> </ol> <p><b>Write with relative/current position example</b> </p> <p>The example below shows an application writing to locations greater than 2GB–1. The <xref href="GUID-BE0804F6-4375-3C8A-8C83-968F510466E0.dita"><apiname>RFile</apiname></xref> class has been replaced with <xref href="GUID-83A415A0-F417-3CA5-BA10-5AEF119AB1F7.dita"><apiname>RFile64</apiname></xref> and the <xref href="GUID-7A2A43EC-6125-3BFE-834B-23C37F7B40D5.dita"><apiname>TInt</apiname></xref> variable that represents the number of bytes written has been changed to <xref href="GUID-AAE85115-A8AA-3F6C-BA8C-69F5A23B0D90.dita"><apiname>TInt64</apiname></xref>. </p> <codeblock id="GUID-2EA7CE6A-C264-54A4-98C8-9AD9C57CBB05" xml:space="preserve">    // 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&lt;1024&gt; 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();
        }</codeblock> </section> <section><title>Write() with absolute position</title> <p>There are four <xref href="GUID-BE0804F6-4375-3C8A-8C83-968F510466E0.dita#GUID-BE0804F6-4375-3C8A-8C83-968F510466E0/GUID-7CA5896D-6FC2-32F4-96F8-864BDAC9861C"><apiname>RFile::Write</apiname></xref> overload functions with absolute position that allow you to specify a position greater than 2GB–1 by using the <xref href="GUID-AAE85115-A8AA-3F6C-BA8C-69F5A23B0D90.dita"><apiname>TInt64</apiname></xref> parameter. </p> <ul><li id="GUID-D9692C56-D4D1-5068-AE4F-0D8209ABE2B5"><p> <xref href="GUID-83A415A0-F417-3CA5-BA10-5AEF119AB1F7.dita#GUID-83A415A0-F417-3CA5-BA10-5AEF119AB1F7/GUID-F2DDC64C-2BB3-3944-8377-F2FCAFF2C36B"><apiname>RFile64::Write(TInt64 aPos, const TDesC8&amp;
                aDes)</apiname></xref>  </p> </li> <li id="GUID-1C749FBD-5403-59A1-8443-5144B94DBC29"><p> <xref href="GUID-83A415A0-F417-3CA5-BA10-5AEF119AB1F7.dita#GUID-83A415A0-F417-3CA5-BA10-5AEF119AB1F7/GUID-52602FFB-4319-3148-A559-C660C59D5409"><apiname>RFile64::Write(TInt64 aPos, const TDesC8&amp; aDes,
                TRequestStatus&amp; aStatus)</apiname></xref>  </p> </li> <li id="GUID-8D20B3ED-DE4A-5C77-8453-CDC221CDCBD3"><p> <xref href="GUID-83A415A0-F417-3CA5-BA10-5AEF119AB1F7.dita#GUID-83A415A0-F417-3CA5-BA10-5AEF119AB1F7/GUID-9834C13E-50A4-3659-B301-C89C6F71C4CA"><apiname>RFile64::Write(TInt64 aPos, const TDesC8&amp; aDes, TInt
                aLength)</apiname></xref>  </p> </li> <li id="GUID-90B8A6B7-15E0-5ADF-98DA-889D2994E9FB"><p> <xref href="GUID-83A415A0-F417-3CA5-BA10-5AEF119AB1F7.dita#GUID-83A415A0-F417-3CA5-BA10-5AEF119AB1F7/GUID-9A3724AB-9B48-35F4-AE97-CB77AC2D1A1B"><apiname>RFile64::Write(TInt64 aPos, const TDesC8&amp; aDes, TInt
                aLength, TRequestStatus&amp; aStatus)</apiname></xref>. </p> </li> </ul> <ol id="GUID-2A682DB4-5D52-5EE1-96CC-AFD56B577E5C"><li id="GUID-D0169894-B983-5C3A-A0E6-CA76BB48EA7F"><p>Use a <xref href="GUID-AAE85115-A8AA-3F6C-BA8C-69F5A23B0D90.dita"><apiname>TInt64</apiname></xref> instead of a <xref href="GUID-7A2A43EC-6125-3BFE-834B-23C37F7B40D5.dita"><apiname>TInt</apiname></xref> for the write position. </p> <p><codeblock id="GUID-EE36228D-DDFD-52AD-8BA4-0C8AF38C7DD2" xml:space="preserve">// TInt writePosition = 0;
    TInt64 writePosition = 0;</codeblock> </p> </li> <li id="GUID-709912EA-32E0-5E7B-A187-7C39AEB13373"><p>Replace RFile with RFile64. </p> <p><codeblock id="GUID-EA9C17D0-64D1-571F-BEC2-71574B843148" xml:space="preserve">// RFile file;
    RFile64 file;</codeblock> </p> </li> </ol> <p><b>Write with absolute position example</b> </p> <p>To write to locations beyond 2GB–1 the file must be opened for 64-bit access and <xref href="GUID-7A2A43EC-6125-3BFE-834B-23C37F7B40D5.dita"><apiname>TInt</apiname></xref> variables should be replaced with <xref href="GUID-AAE85115-A8AA-3F6C-BA8C-69F5A23B0D90.dita"><apiname>TInt64</apiname></xref>. </p> <codeblock id="GUID-27FAE59A-F150-54B9-AC95-BA588552E605" xml:space="preserve">    // 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&lt;1024&gt; 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
        }</codeblock> </section> </conbody></concept>