文件创建与读写练习
RFile 可以读写一个文件,在 create 一个文件时,如果已存在同名的文件,则会出错,可以选择用 replace 方法
RFile 使用简单,注意的是读写的方法有多种,其中涉及到 8 位与16位转换的问题,用完后需要 close(),以下面练习代码
void writeFile() { User::LeaveIfError(ifs.Connect()); RFile file; _LIT(KfileName,"c:\\mytxtfile\\aaa.txt"); TBufC<20> filename(KfileName); // RFs ifs; 这个在前面已定义 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(); } // 写完后,读出来 /* 读出来时要转为 16 位保存,否则显示乱码 */ TBuf8<4> aRead; TBuf<4> aread16; file.Open(ifs,filename,EFileRead); _LIT(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(); } |
关于 RFile 的说明,请看 SDK的帮助
Location:
SupportSupported from 5.0 DescriptionCreates and opens a file, and performs all operations on a single open file. These include:
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:
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 both 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 ( Derivation
MembersDefined in Inherited from Inherited from |
安平2009@原创
qi_jianzhou@126.com