How to read a multiline file
Article Metadata
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
- Another option is to use TFileText. For a description see TFileText in Base F32_EKA2
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.
- Also check this article for how to use TFileText to read the contents of file, line by line: How to retrieve S60 5th Edition device model name


Nice article to read file line by line, but need some improvements. First of all please avoid use of TBuf<30> for storing characters of line, what happen if line is longer than 30 character?? Second thing title of this article should be something like "How to read a file line by line" because other methods of RFile read all (multiple) line at a time and so that methods are more suitable to read multiline file.