RFile is the base API for file reading and writing, it is quite simple to use as following code sample shows:
HBufC8* ReadFromFileL(const TDesC& aFile) { HBufC8* FilBuff(NULL); RFile ReadFil; if(KErrNone == ReadFil.Open(CCoeEnv::Static()->FsSession(),aFile,EFileRead)) { CleanupClosePushL(ReadFil); TInt FilSiz(0); if(ReadFil.Size(FilSiz) == KErrNone) { if(FilSiz > 0) { FilBuff = HBufC8::NewL(FilSiz); TPtr8 ReadPoint(FilBuff->Des()); ReadFil.Read(ReadPoint,FilSiz); } } CleanupStack::PopAndDestroy(1);//ReadFile } return FilBuff; }
With this function you can read the whole content of the file into a HBufC byte buffer. Note that the name has to be full name (drive+path+name+extension). One problem with this approach is that if the file is open it will not work, then instead you could use the RFs API directly as shown in this sample:
HBufC8* ReadOpenFileL(const TDesC& aFile) { HBufC8* FilBuff(NULL); TEntry MyEntry; User::LeaveIfError(CCoeEnv::Static()->FsSession().Entry(aFile,MyEntry)); if(MyEntry.iSize > 0) { FilBuff = HBufC8::NewL(MyEntry.iSize); TPtr8 ReadPoint(FilBuff->Des()); CCoeEnv::Static()->FsSession().ReadFileSection(aFile,0,ReadPoint,FilSiz); } return FilBuff; }
The ReadFileSection reads the file without opening the file. And to write a file, you could use the RFile API by doing something like this:
void WriteToFileL(const TDesC& aFile,const TDesC8& aData) { RFile ReadFil; if(BaflUtils::FileExists(CCoeEnv::Static()->FsSession(),aFile)) User::LeaveIfError(ReadFil.Open(CCoeEnv::Static()->FsSession(),aFile,EFileWrite)); else User::LeaveIfError(ReadFil.Create(CCoeEnv::Static()->FsSession(),aFile,EFileWrite)); CleanupClosePushL(ReadFil); User::LeaveIfError(ReadFil.Write(aData)); CleanupStack::PopAndDestroy(1);//ReadFile }
This implementation appends to the existing file or creates new file if file does not exist. In case you want to replace already existing file, then you can use RFs API’s Delete()-function to delete the file or Replace()-function from the RFile.
| Thread | Thread Starter | Forum | Replies | Last Post |
|---|---|---|---|---|
| USER 44 while reading large files | sajain | General Symbian C++ | 3 | 2008-02-05 12:12 |
| How to access Logdbu.dat | chavasekhar | General Symbian C++ | 0 | 2003-05-12 08:32 |
| GPS data via bluetooth | dmulvang | Python | 1 | 2006-10-04 10:01 |
| Memory Storage MT? | nekrecart | PC Suite API and PC Connectivity SDK | 1 | 2003-08-28 18:27 |
| Does Nokia N70 supports AT commands for reading SMS | skylark_khur | General Messaging | 4 | 2007-02-23 13:32 |