File Creation and read/write exercises

Source: Internet
Author: User

 

File Creation and read/write exercises

RFile can read and write a file. If a file with the same name already exists during file creation, an error will occur. You can use the replace method.

RFile is easy to use. Note that there are multiple read and write methods, which involve the conversion of 8-bit and 16-bit data. close () is required after use. The following is the practice code.

Void writeFile ()
{
User: LeaveIfError (ifs. Connect ());
RFile file;
_ Partition (KfileName, "c :\\ mytxtfile \ aaa.txt ");
TBufC <20> filename (KfileName );
// RFs ifs; defined previously
If (file. Create (ifs, filename, EFileWrite )! = KErrAlreadyExists)
{
_ LIT8 (KFileContent, "Test File write ");
TBufC8 <20> bufc (KFileContent );
User: LeaveIfError (file. Write (KFileContent ));
User: LeaveIfError (file. Flush ());
File. Close ();
}
// Read it after writing
/*
When reading data, convert it to 16-bit storage; otherwise, garbled characters are displayed.
*/
TBuf8 <4> aRead;
TBuf <4> aread16;

File. Open (ifs, filename, EFileRead );
_ Partition (KC, "% S \ n ");
TInt I = 0;
Do
{
ARead. Zero ();
Aread16.Zero ();
File. Read (I, aRead, 4 );
I + = 4;
File. Seek (ESeekStart, I );
Aread16.Copy (aRead );
Console-> Printf (KC, & aread16 );
} While (aRead. Length ()! = 0 );

File. Close ();
Ifs. Close ();
}

 

For more information about RFile, see the SDK help.

Location:f32file.h
Link against:efsrv.lib

ClassRFile

RFile

Support

Supported from 5.0

Description

Creates and opens a file, and performs all operations on a single open file. These include:

  • Reading from and writing to the file

  • Seeking to a position within the file

  • Locking and unlocking within the file

  • Setting file attributes

Before using any of these services, a connection to a file server session must have been made and the file must be open.

Opening Files:

  • UseOpen()To open an existing file for reading or writing-an error is returned if it does not already exist. To open an existing file for reading only, useOpen()With an access modeEFileReadAnd a share modeEFileShareReadersOnly.

  • UseCreate()To create and open a new file for writing-an error is returned if it already exists.

  • UseReplace()To open a file for writing, replacing any existing file of the same name if one exists or creating a new file if one does not exist,

  • UseTemp()To create and open a temporary file with a unique name, for writing and reading.

When opening a file, you must specify the file server session to use for operations with that file. if you do not close the file explicitly, it is closed when the server session associated with it is closed.

Reading and Writing:

There are several variants of bothRead()AndWrite(). The basicRead(TDes8& aDes)AndWrite(const TDesC8& aDes)Are supplemented by variants allowing the descriptor length to be overridden, or the seek position of the first byte to be specified, or asynchronous completion, or any combination.

Reading transfers data from a file to a descriptor, and writing transfers data from a descriptor to a file. In all cases, the file data is treated as binary and byte descriptors are used (TDes8,TDesC8).

 

Derivation
    • RSubSessionBase-Client-side handle to a sub-session

      • RFsBase-Base class that provides a Close () function for file related clean-up

        • RFile-Creates and opens a file, and performs all operations on a single open file
    Members

    Defined inRFile:
    Att(),ChangeMode(),Create(),Flush(),Lock(),Modified(),Open(),Read(),Read(),Read(),Read(),Read(),Read(),Read(),Read(),Rename(),Replace(),Seek(),Set(),SetAtt(),SetModified(),SetSize(),Size(),Temp(),UnLock(),Write(),Write(),Write(),Write(),Write(),Write(),Write(),Write()

    Inherited fromRFsBase:
    Close()

    Inherited fromRSubSessionBase:
    CloseSubSession(),CreateSubSession(),Send(),SendReceive(),SubSessionHandle(),operator=()

    Anping 2009 @ original
    Qi_jianzhou@126.com

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.