Symbian3/SDK/Source/GUID-FA6E3270-C20A-5B05-9FFC-F3D4D8B35999.dita
author Dominic Pinkman <Dominic.Pinkman@Nokia.com>
Thu, 21 Jan 2010 18:18:20 +0000
changeset 0 89d6a7a84779
permissions -rw-r--r--
Initial contribution of Documentation_content according to Feature bug 1266 bug 1268 bug 1269 bug 1270 bug 1372 bug 1374 bug 1375 bug 1379 bug 1380 bug 1381 bug 1382 bug 1383 bug 1385

<?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-FA6E3270-C20A-5B05-9FFC-F3D4D8B35999"><title>How to create and open a file</title><prolog><metadata><keywords/></metadata></prolog><conbody><p>The <xref href="GUID-BE0804F6-4375-3C8A-8C83-968F510466E0.dita"><apiname>RFile</apiname></xref> interface is used to create, open, read from and write to a single file. </p> <section><title>Creating and opening a file</title> <p>A common pattern is to attempt to open an existing file, without replacing its existing data, and create it if it does not exist. </p> <ol id="GUID-42702312-C0C9-5788-8819-4BB5730320B5"><li id="GUID-31FFB1C8-AB53-581B-835E-0C33839CEEA5"><p>Use <codeph>Open()</codeph> to open an existing file for reading or writing - an error is returned if it does not already exist. </p> </li> <li id="GUID-9CC8F1EA-196E-5A77-AA80-9BBA198C3B62"><p>Use <codeph>Create()</codeph> to create and open a new file for writing. </p> </li> </ol> <codeblock id="GUID-70C09A68-4C30-5766-99C5-D9BF23ADC85F" xml:space="preserve">TInt err=file.Open(fsSession,fileName,shareMode);
if (err==KErrNotFound) // file does not exist - create it
    err=file.Create(fsSession,fileName,shareMode);</codeblock> </section> <section><title>Specifying a share and access mode</title> <p>Use various flags to specify how a file is opened. </p> <codeblock id="GUID-44FB8760-7C13-503D-A53A-3DF794768D26" xml:space="preserve">
// Open a file as text, writable, and shared with other clients
RFile file;
_LIT(KFileName, "FILENAME.CPP");
file.Open(fsSession,KFileName,EFileStreamText|EFileWrite|EFileShareAny);</codeblock> <p><b>Notes</b> </p> <ul><li id="GUID-6124BD90-9267-523E-BC8E-590BED022083"><p>If another <codeph>RFile</codeph> object tries to open this file in <codeph>EFileShareExclusive</codeph> or <codeph>EFileShareReadersOnly</codeph> mode, access is denied. It can only be accessed in <codeph>EFileShareAny</codeph> mode. </p> </li> <li id="GUID-45948CEF-7780-51E4-B862-F7A8A5983EE5"><p>If a file is not closed explicitly (using <codeph>RFile::Close()</codeph>), it will be closed when the server session associated with it is closed. </p> </li> </ul> </section> </conbody></concept>