Currently Being Moderated

Try 5: Page 8

VERSION 1

Created on: Mar 5, 2008 11:44 AM by Curl Education - Last Modified:  Jul 3, 2008 3:34 PM by Curl Education

!../../wiki/easy-ide-book/common/images/prev_page.gif! !../../wiki/easy-ide-book/common/images/toc.gif! 

 

 

 

Extra Practice

 

 

Practice Problem 1: Basics

 

 

Let’s write a program that writes the data displayed by <font color="purple">RecordGrid</font> into a file. The program must satisfy the following requirements:

 

  • The location to which the file is saved is to be specified by the user, using the Save as dialog box.

  • The data displayed using <font color="purple">RecordGrid</font> will have three fields: name, age, and score.

  • The overwriting of data in an existing file shall be prohibited.

 

 

 

 

Hint: Use commas to delimit each of the fields of the data in each <font color="purple">Record</font>, and then write each <font color="purple">Record</font> to one line of the file.

 

Solution

 

 

Solution Program: c:\Curl\Try5\04_exercise1\start.curl

 

{value
    let out:#TextOutputStream
    let data:RecordSet = {RecordSet
                             {RecordFields
                                 {RecordField name, domain = String},
                                 {RecordField age, domain = int},
                                 {RecordField score, domain = int}
                             },
                             {RecordData name = Matt, age = 33, score = 88},
                             {RecordData name = Sarah, age = 27, score = 79},
                             {RecordData name = Jacob, age = 26, score = 90}
                         }
    
    let cb:CommandButton = {CommandButton
                               label = Save,
                               {on Action do
                                   {if-non-null loc = {choose-file style = save-as} then
                                       {try
                                           set out = {write-open
                                                         error-if-exists? = true,
                                                         loc
                                                     }
                                           {for r:Record in data do
                                               {out.write-one-string
                                                   {format "%s , %s, %s \r\n, 
                                                       r[name], r[age], r[score]}
                                               }
                                           }
                                        catch e:IOException do
                                           {popup-message Error writing data"}
                                           {output e.message}
                                        finally
                                           {if-non-null out then
                                               {out.close}
                                           }
                                       }
                                   }
                               }
                           }
    {VBox
        {RecordGrid
            record-source = data,
            width = 10cm,
            height = 3cm
        },
        cb
    }
}

 

 

Explanation

 

 

<font color="purple">RecordSet</font>s, like arrays, are collections, so a <font color="purple">RecordSet</font> can be passed to a <font color="purple">for</font> loop, which will process the contained <font color="purple">Record</font>s in order.

 

!../../wiki/easy-ide-book/common/images/toc.gif!

Average User Rating
(0 ratings)




There are no comments on this document

More Like This

  • Retrieving data ...