Mobiiliohjelmointi/Mobile programming
Exercise 6 – File server
& files
This
exercise focuses on how to use file server and read data from a text file.
Download application: Animation for SDK 3rd FP1(zip
file) that is used as a basis for the exercise. NOTE: if you are
using older Symbian SDK, here is same application : Animation for SDK 2nd FP 3
The goal of the
exercise is to modify animation application so that amount of animated objects
and the coordinates (circle shapes in the picture) are read from a text
file.
settings.txt (UNICODE)
10,10
20,20
25,30
30,40
Part 1 - read data from a text file
- Create a UNICODE file Settings.txt to the folder
"Symbian\8.1\S60_2nd_fp3\Epoc32\winscw\c\Documents\Animation"
- Connect file server and open a text file
(CAEng::ReadDataFromFile)
- Declare following objects: RFs fsSession,
RFile msgFile, TFileText txtFile
- Connect to the file server, leave if fails
- Push file server into the cleanup stack
- Use: "C:\\Documents\\Animation\\Settings.txt"
as a constant literal for the file name
- Use BaflUtils::FileExists to check if the file is
there
- Open the file using RFile::Open, leave if this
fails
- Push file into cleanup stack
- Use TFileText::Set to make it use the RFile just
opened
- Seek to the beginning of file using TFileText::Seek
- Read lines from file
- Read a single line into a TBuf<256> descriptor
using TFileText::Read
- Use while-loop to read all lines from the file
- Parse a single line
- Write a new member function void ParseLine(TDes&
aLine)
- Declare a local variable TLex lexer(aLine);
- Use TLex.SkipSpaceAndMark to set the next
character position to its exraction mark
- Read characters in a while-loop using TLex.Get
until you find a separator (comma)
- Unread the last comma using TLex.UnGet
- Read the x coordinate using TPtrC TLex.MarkedToken
- Skip comma using TLext.SkipAndMark(1);
- Read the y coordinate using TLex.Remainder;
- Convert string to int and insert coordinates to the array
- Convert string to integer using TLex.Val
- Append coordinates to the iObjArray using TPoint
type.
- Close file and file server
- Pop and destroy the File from CleanupStack
- Pop and destroy the file server from CleanupStack