Namespaces
Variants
Actions
Revision as of 09:19, 13 September 2012 by hamishwillee (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to read a multiline file

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.


235 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved