×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    8

    read integer values out of a text file

    hi all!

    i try to read integer values out of a simple text file. there is only one value in each line of the file. so i tried the following:


    TBuf<64> readbuffer = _L("");

    TLex templex;
    TInt tempvalue;

    RFs filesession;
    User::LeaveIfError(filesession.Connect());
    CleanupClosePushL(filesession);

    RFile file;
    User::LeaveIfError(file.Open(filesession, txtpath, EFileShareReadersOnly|EFileRead));
    CleanupClosePushL(file);

    TFileText textfilehandle;
    textfilehandle.Set(file);
    textfilehandle.Seek(ESeekStart);

    error = textfilehandle.Read(readbuffer);
    templex.Assign(readbuffer);
    templex.Val(tempvalue);
    intValue = tempvalue;

    ...


    the documentation says that TFileText::Read() always reads one line out of the file. but it seems that it reads as much as possible.


    how can i read single lines out of a text file? and how can i convert that into an integer?


    thx in advance for any hints!

    mugi

  2. #2
    Registered User
    Join Date
    Feb 2005
    Location
    Turkey & Finland
    Posts
    42
    I'm not sure about this but, new line in windows might be different than newline in Symbian. For example new line is \r\n in windows and \n in Linux. So your program might work on a symbian device. But I guess this cannot be the case. The guys in Symbian should be smart enough to write platform independent read function

    You can try to use RReadStream instead.
    There are functions that read int from stream such as ReadInt16L(), ReadInt32L(), ReadInt8L().

    I hope this helps.
    --------------------------------------------------------------------------------
    CaNeR

  3. #3
    Nokia Developer Expert
    Join Date
    Apr 2003
    Location
    Finland
    Posts
    425
    Hi,

    The version you are using on your example code uses unicode text to be read. Check you file is on unicode format, if it is "simple text file" i assume it is ascii file, e.g. wrote by notepad, and therefore cannot be read within TInt Read(TDes& aDes); becase TDes means 16 bit unicode characters (becase you are using TBuf<64> which is 16 bit by default)

    If your file is generated by symbian software my suggestion is to read/write with streams as suggested. It is more efficient way to go.

    Br V

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    8
    hi veskola!

    thx for your hint! with unicode it works fine!

    unfortunately i have to read files not generated by symbian but by windows or linux.

    thx also to LAS__VEGAS! perhaps you could post a code sample with RReadStream dealing with my problem? i didn't really understand how to play around with streams when reading the documantation ...


    greez
    mugi

  5. #5
    Nokia Developer Expert
    Join Date
    Apr 2003
    Location
    Finland
    Posts
    425
    Hi,

    Streams are typically used only for files which are also written by stream classes and symbian devices.

    My suggestion is to use RFile class and the method:
    Code:
    TInt Read(TDes8& aDes) const;
    I haven't use it myself, but it should read 8 bit ascii becase the parameter is 8 bit character.

    I think you will get something like:
    Code:
    TBuf8<255> buffer;
    RFile theFile;
    theFile.Open(...);
    theFile.Read(buffer);
    -> buffer: [0x65 0x66 0x67 ...] ' ABC...
    Br V

Posting Permissions

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