Read-only archive of the All About Symbian forum (2001–2013) · About this archive

Reading and writing files.

5 replies · 10,782 views · Started 20 January 2003

Hi, as I am pretty new to this subject I would like to have some help about file manipulation.

I am using this code snippet and I am able to write data into the file and I can read it but how do I display the data?

I know that it works cause I can get the length with Length().
Is this the preferred way to do it on Series60?

Thanks for all possible answers !

[code:1]_LIT( KWorksDataFile,"C:\\System\\apps\\Image\\log.txt" );

RFs fsSession;
RFile file;
TFileText txt_file;

User::LeaveIfError(fsSession.Connect());

TInt err=file.Open(fsSession,KWorksDataFile,EFileStreamText|EFileWrite|EFileShareAny);
if (err==KErrNotFound) // file does not exist - create it
err=file.Create(fsSession,KWorksDataFile,EFileStreamText|EFileWrite|EFileShareAny);

_LIT8(KWriteBuf,"Writing data into file");
file.Write(KWriteBuf);

TBuf8<50> readBuf1;
file.Read(0,readBuf1); [/code:1]

Just display it as you would with any text string...??

But seems that you need to convert the TBuf8 to TBuf16 first for Series 60 platform.

Edited You can try with UserInfoPrint() on emulator to see if it display the correct data you wish to get from file.

Thanks Khanming !

I copied the information from TBuf8 -> TBuf and waas able to display the data as I wanted 😊

But another question why cant I read the data directly into a TBuf ??

Is there other ways of doing this ?

(You're welcome!)

This question is better answered by the SDK Help itself:


In all cases, the file data is treated as binary and byte descriptors are used (TDes8, TDesC8). Thus, file operations are not dependent on the Unicode/non-Unicode build variant.

There are several variants of both Read() and Write(). The basic Read(TDes8& aDes) and Write(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.

Just my experience: You can use RFileReadStream class in combination with RFile to read file. This class provides ReadL() that accept both 8 bit and 16 bit variants, as well as support for reading/writing of integers.
(Use RFileWriteStream for the other way round.)

[code:1]_LIT( KWorksDataFile,"C:\\System\\apps\\Image\\log.txt" );

RFs fsSession;
RFile file;
TFileText txt_file;

User::LeaveIfError(fsSession.Connect());

TInt err=file.Open(fsSession,KWorksDataFile,EFileStreamText|EFileWrite|EFileShareAny);
if (err==KErrNotFound) // file does not exist - create it
err=file.Create(fsSession,KWorksDataFile,EFileStreamText|EFileWrite|EFileShareAny);

tfile.Set(file);
tfile.Write(_L("Writing data into file"));//Write seeks to end of file
tfile.Seek(ESeekStart);//Seek to beginning
TBuf<256> readBuf1;
tfile.Read(readBuf1);
fsSession.Close();[/code:1]

This code should work and is easy to use if you need to read and write lines of text to or from a file.

iEikonEnv->InfoMsg(readBuf1);

Can be used to display the data.

Hello,

I would like to read a file a line at a time. My code crashes when I attempt to read the last line since it ends with an EOF rather than a \n and I cannot TRAP the error. The program halts with an epoc error.

TInt fSize;
TInt iReadBytes = 0;
RFile file;
RFs fsSession;
_LIT( KContactFile,"\\system\\Apps\\PSCLIENT\\contacts.txt"😉;
User::LeaveIfError(fsSession.Connect());
TInt err = file.Open(fsSession,KContactFile, EFileRead);
if (err != KErrNotFound)
{
// read the Contact file
file.Size(fSize);
RFile f = file;
RFileReadStream stream(f);
TBuf8<128> *buf = new(ELeave)TBuf8<128>;
do {
TRAPD(err, stream.ReadL(*buf,(TChar)'\n'😉);
if (err == KErrEof)
{
int i = 1;
}
iReadBytes += buf->Length();
buf->Delete(0, 128);
} while (iReadBytes < fSize);
stream.Close();
fsSession.Close();
}