06-20-2011, 01:21 PM
Structure of the program that reads our score data
In this example, the data that is read from the file is displayed using a RecordGrid.
1. Specifying the location of the file
Specify the location of the file.
2. Declaration of a TextInputStreams
To read data from the file, we have to declare TextInputStream.
3. Declaration of character string array used to save read-in data
Declare the character string array to be used for the data read from a file.
4. Declaration of RecordSet used to store data
In this sample, the data obtained from a file is converted to a RecordSet, and then later displayed using RecordGrid.
5. Opening a file
Using read-open, we open the specified file as a TextInputStream.
6. Reading the data
The data is read, line-by-line, and then added to the character string array.
7. Closing the file
Regardless of whether the data read from the file is successful or not, the open file must be closed.
8. Data conversion
We have to convert the data that is stored in a StringArray to a RecordSet. Each line of data in the file corresponds to one record. Also, each field is delimited by a comma (,). So, using the String.split method and a comma (,) as the delimiter, we partition the data and then save it into an array. Using the data in that array, we create RecordData, and then add it to a RecordSet.
9. Displaying the layout
We take the RecordSet that we created in (8) and then display it using a RecordGrid.
Working with CSV Data
The Curl language also provides a predefined API for reading and writing CSV (comma separated values) formatted files. Using CsvRecordSet, you can load data directly as a RecordSet for further manipulation and/or display. Please refer to the Curl documentation for more information
In this example, the data that is read from the file is displayed using a RecordGrid.
1. Specifying the location of the file
Code:
let loc:Url = {url score.txt}
Specify the location of the file.
2. Declaration of a TextInputStreams
Code:
let in:#TextInputStream
To read data from the file, we have to declare TextInputStream.
3. Declaration of character string array used to save read-in data
Code:
let data-array:StringArray = {StringArray}
Declare the character string array to be used for the data read from a file.
4. Declaration of RecordSet used to store data
Code:
let record-set:RecordSet =
{RecordSet
{RecordFields
{RecordField name, domain = String},
{RecordField age, domain = int},
{RecordField score, domain = int}
}
}
In this sample, the data obtained from a file is converted to a RecordSet, and then later displayed using RecordGrid.
5. Opening a file
Code:
set in = {read-open loc}
Using read-open, we open the specified file as a TextInputStream.
6. Reading the data
Code:
{until in.end-of-stream? do
{if-non-null line = {in.read-line} then
{data-array.append {line.to-String}}
}
}
The data is read, line-by-line, and then added to the character string array.
7. Closing the file
Code:
{if-non-null in then
{in.close}
}
Regardless of whether the data read from the file is successful or not, the open file must be closed.
8. Data conversion
Code:
{for data:String in data-array do
let array:StringArray = {data.split split-chars = ,}
{if array.size == 3 then
{record-set.append
{RecordData
name = array[0], age = {array[1].to-int}, score = {array[2].to-int}
}
}
}
}
We have to convert the data that is stored in a StringArray to a RecordSet. Each line of data in the file corresponds to one record. Also, each field is delimited by a comma (,). So, using the String.split method and a comma (,) as the delimiter, we partition the data and then save it into an array. Using the data in that array, we create RecordData, and then add it to a RecordSet.
9. Displaying the layout
Code:
{RecordGrid
record-source = record-set,
width = 10cm,
height = 3cm
}
We take the RecordSet that we created in (8) and then display it using a RecordGrid.
Working with CSV Data
The Curl language also provides a predefined API for reading and writing CSV (comma separated values) formatted files. Using CsvRecordSet, you can load data directly as a RecordSet for further manipulation and/or display. Please refer to the Curl documentation for more information