×

Discussion Board

Results 1 to 12 of 12
  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    338

    Problem reading simple text file

    Hi,
    I've use the folloing code given in an example( in this forum) to read text file.
    Code:
    _LIT(KFilename,"Z:\\resource\\versions\\product.txt");
    RFile file;
    User::LeaveIfError(file.Open(CCoeEnv::Static()->FsSession(),KFilename,EFileRead));
    CleanupClosePushL(file);
    TFileText text;
    text.Set(file);
    TBuf16<128> szModelName;
    for(TInt i=0; i<2; i++) // TFileText::Read() reads one line at a time and our model information is in the second line.
    {
        User::LeaveIfError(text.Read(szModelName));
    }
    CleanupStack::PopAndDestroy(&file);
    CAknInformationNote* note = new (ELeave) CAknInformationNote(ETrue);
    note->ExecuteLD(szModelName);
    My problem is when i tried to read a file containing simple text "abcdefgh" and use RDebug::Print() to see the read line in console it shows "aceg" that means it is missing each character one after another. How to solve it??
    Thank you.

  2. #2
    Registered User
    Join Date
    Dec 2009
    Posts
    339

    Re: Problem reading simple text file

    TFileText reads the texts in unicode format. U may try to convert it into ascii format using CnvUtfConverter::ConvertFromUnicodeToUtf8( DestinationBuffer8, SourceBuffer16 );

  3. #3
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,740

    Re: Problem reading simple text file

    Indeed, TFileText is rarely usable for anything.
    Reading long ASCII/UTF-8 files in an efficient way is a difficult task.
    However files in z:\resource\versions are small enough to read them completely
    Code:
    TInt size;
    User::LeaveIfError(file.Size(size));
    HBufC8 *buf8=HBufC8::NewLC(size);
    TPtr8 ptr8=buf8->Des();
    User::LeaveIfError(file.Read(ptr8));
    Then use TDesC::Locate and Mid methods to get the second line.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    338

    Re: Problem reading simple text file

    Quote Originally Posted by wizard_hu_ View Post
    Indeed, TFileText is rarely usable for anything.
    Reading long ASCII/UTF-8 files in an efficient way is a difficult task.
    However files in z:\resource\versions are small enough to read them completely
    Code:
    TInt size;
    User::LeaveIfError(file.Size(size));
    HBufC8 *buf8=HBufC8::NewLC(size);
    TPtr8 ptr8=buf8->Des();
    User::LeaveIfError(file.Read(ptr8));
    Then use TDesC::Locate and Mid methods to get the second line.
    It may solve but i want to save memory. To read some lines of a file i don't want to load the whole file right now. I decided to use fgets but i am not familiar how to use it in symbian

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    338

    Re: Problem reading simple text file

    Quote Originally Posted by slinx View Post
    TFileText reads the texts in unicode format. U may try to convert it into ascii format using CnvUtfConverter::ConvertFromUnicodeToUtf8( DestinationBuffer8, SourceBuffer16 );
    Though it should have worked but i don't know why RDebug::Print() is showing strange characters now.
    Thank you

  6. #6
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,740

    Re: Problem reading simple text file

    If there is a line break character at the end of the second line too (because there is a third line too, or just for "being nice" reasons), you can use RFileReadStream. It has a ReadL method accepting delimiter character, where you can pass the line break character.

  7. #7
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,740

    Re: Problem reading simple text file

    Quote Originally Posted by Md. Kayesh View Post
    Though it should have worked but i don't know why RDebug::Print() is showing strange characters now.
    It may require a bit of experience, but otherwise it is more or less trivial. RDebug::Print is probably not prepared to handle "real" Unicode characters, and it simply drops the upper byte of them. It is a debug tool after all. And TFileText reads files as if they are in "lower byte, upper byte" byte order. So it reads two byte "characters" as ab, cd, ef, gh, then RDebug drop the second bytes of them, and what remains is aceg.

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    338

    Re: Problem reading simple text file

    Quote Originally Posted by wizard_hu_ View Post
    If there is a line break character at the end of the second line too (because there is a third line too, or just for "being nice" reasons), you can use RFileReadStream. It has a ReadL method accepting delimiter character, where you can pass the line break character.
    That's a nice idea. Could you kindly post an example or give a link to any code? Actually i want to create a function that that will take a range of line number like 10th line to 15th line and the function will read those 6 lines from that pointed file and send those strings altogether.
    Thank you.

  9. #9
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,740

    Re: Problem reading simple text file

    Quote Originally Posted by Md. Kayesh View Post
    That's a nice idea. Could you kindly post an example or give a link to any code? Actually i want to create a function that that will take a range of line number like 10th line to 15th line and the function will read those 6 lines from that pointed file and send those strings altogether.
    RFileReadStream::Open has exactly the same syntax as RFile::Open. The given ReadL method is invoked as "stream.ReadL(line,'\n');" where line is a TBuf8<100> or something like that (you will still need an estimation for longest possible line).

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    338

    Re: Problem reading simple text file

    Quote Originally Posted by wizard_hu_ View Post
    RFileReadStream::Open has exactly the same syntax as RFile::Open. The given ReadL method is invoked as "stream.ReadL(line,'\n');" where line is a TBuf8<100> or something like that (you will still need an estimation for longest possible line).
    Yes. I did it but when i am printing using RDebug::Print() as i stated ago, it is missing alternate characters instead of printing "abcdefgh" it is printing "aceg IIIIII". How to avoid this problem? Another thing is how can i know that i've reached the last line or how to avoid Kerreof while reading last line?
    Thank you.

  11. #11
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,740

    Re: Problem reading simple text file

    Quote Originally Posted by Md. Kayesh View Post
    Yes. I did it but when i am printing using RDebug::Print() as i stated ago, it is missing alternate characters instead of printing "abcdefgh" it is printing "aceg IIIIII". How to avoid this problem?
    Note that if you are passing 16-bit descriptors to stream.ReadL, that repeats the issue with TFileText
    Another thing is how can i know that i've reached the last line or how to avoid Kerreof while reading last line?
    This is why I called it difficult. I do not know about any ready-made solution. TFileText is practically unusable, and the RReadStream::ReadL(buf,delim) variant loses the content following the last delimiter.
    You can try opening the RFile yourself, pass it to RFileReadStream::Attach, and record the current file offset after each succesful ReadL (hopefully it is updated, you can get the position via RFile::Seek+ESeekCurrent, just always zero the position beforehand, because it is a two-way argument). After that you should TRAP the entire loop, and at the end (when KErrEof comes), seek back to the last position (ESeekStart and pass the stored position argument), and Read once.
    Code:
    RFile file;
    User::LeaveIfError(file.Open(...));
    CleanupClosePushL(file);
    TBuf8<100> line;
    TInt seek;
    TRAPD(err,
        RFileReadStream stream;
        stream.Attach(file);
        stream.PushL();
        while(ETrue)
        {
            seek=0;
            User::LeaveIfError(file.Seek(TSeek::ESeekCurrent,seek));
            stream.ReadL(line,'\n');
            [do something with line]
        });
    if(err==KErrEof)
    {
        User::LeaveIfError(file.Seek(TSeek::ESeekStart,seek));
        User::LeaveIfError(file.Read(line));
        [do something with line]
    }
    CleanupStack::PopAndDestroy(); // file

  12. #12
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,740

    Re: Problem reading simple text file

    Well, this approach does not work (besides the compilation problems caused by "TSeek::") because RFileReadStream actually "eats" the file object, resulting in KERN-EXEC 0-s. Also, the '\n' behaves as a length, instead of TChar delmiter.
    However file streams are buffered, so
    Code:
    RFileReadStream frs;
    User::LeaveIfError(frs.Open(...));
    frs.PushL();
    TBuf8<100> line;			
    TStreamPos pos;
    TRAPD(err,
        const TChar KNewLine('\n');
        while(ETrue)
        {
            pos=aFrs.Source()->TellL(MStreamBuf::ERead);
            frs.ReadL(line,KNewLine);
            [do something with line]
        });
    if(err==KErrEof)
    {
        frs.Source()->SeekL(MStreamBuf::ERead,pos);
        frs.ReadL(line,frs.Source()->SizeL()-pos.Offset());
        [do something with line]
    }
    else User::LeaveIfError(err);
    CleanupStack::PopAndDestroy(); // frs
    works.

Similar Threads

  1. Replies: 4
    Last Post: 2010-08-03, 08:04
  2. [moved] problem with text file reading
    By masoud_bayat in forum Symbian
    Replies: 5
    Last Post: 2008-10-05, 08:22
  3. Problem while Reading text file in Nokia3650
    By lalithunt in forum Mobile Java General
    Replies: 5
    Last Post: 2008-07-30, 17:19
  4. Replies: 3
    Last Post: 2007-11-17, 10:31
  5. Problem about Reading Chinese Text file?
    By goeast in forum Mobile Java General
    Replies: 0
    Last Post: 2002-09-18, 03:25

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×