Thread Rating:
  • 278 Vote(s) - 2.73 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Page 6: Structure of a Program that Reads our Score Data
06-20-2011, 01:21 PM, (This post was last modified: 06-23-2011, 04:26 PM by ashimo.)
#1
Page 6: Structure of a Program that Reads our Score Data
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

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


Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  Page 8: Extra Practice ashimo 0 3,482 06-20-2011, 01:34 PM
Last Post: ashimo
  Page 7: Summary ashimo 0 3,046 06-20-2011, 01:23 PM
Last Post: ashimo
  Page 5: Application: Reading Our Score Data ashimo 0 2,775 06-20-2011, 01:20 PM
Last Post: ashimo
  Page 4: Structure of a Program that Writes Data to a File ashimo 0 3,148 06-20-2011, 01:18 PM
Last Post: ashimo
  Page 3: Basics 2: Writing a Control Value into a File ashimo 0 3,022 06-20-2011, 01:16 PM
Last Post: ashimo
  Page 2: Structure of a Program that Reads Data from a File ashimo 0 3,039 06-20-2011, 01:13 PM
Last Post: ashimo
  Page 1: Reading Our Score Data ashimo 0 3,171 06-20-2011, 01:12 PM
Last Post: ashimo
Forum Jump:


Users browsing this thread:
1 Guest(s)

MyBB SQL Error

MyBB has experienced an internal SQL error and cannot continue.

SQL Error:
1017 - Can't find file: 'mybb_threadviews' (errno: 2)
Query:
INSERT INTO mybb_threadviews (tid) VALUES('76')