×
Namespaces

Variants
Actions

How to read a multiline file

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: vdharankar (05 Apr 2010)
Last edited: hamishwillee (13 Sep 2012)

The logic

If you ever had a problem while reading a multi-line file in Symbian c++ then here is a solution. Here the code uses RFile class and mainly its Read() method to read one character at a time, each character is compared with newline. If newline character is found then the line is finished, just store is or process it. Terminate the reading loop using the size of the file. That's the easy way, for this you can not rely on Read() function as even if you call Read() at the EOF still it doesn't return error code, and returns 0 instead. Its little tricky here to terminate the loop :)

Code

RFs fs;
fs.Connect();
RFile file;
file.Open(fs,KFilePath,EFileRead);
TBuf8<2> c;
TBuf<30> line;
TInt i=0;
TInt size=0;
file.Size(size);
 
while(true)
{
i++;
if(i > size)
break;
 
file.Read(c,1);
if(c.Compare(_L8("\n")) == 0)
{
iEikonEnv->AlertWin(line);
line = _L("");
}
else
{
if(c.Length() > 0)
line.Append(c[0]);
}
 
}

Alternatives

The only limitation TFileText class faces is it tries to read the text in UNICODE or UTF8 format and if your file is not stored in that encoding format then you see hex characters instead of the expected text.


This page was last modified on 13 September 2012, at 09:19.
44 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×