STx 3.9 Documentation

Up

Introducing File Items

Previous

Introducing File Items

Next

Section Files

Text Files

S_TOOLS-STx text file items can be used to read and write ASCII and UNICODE text files.

Text file items are created with the option /Text.

Reading and Writing to Text Files

The commands READ and WRITE, and the file item commands LOAD and SAVE can be used to read from and write to text files.

Text File Example

The following script demonstrates the use of a text file item.

[macro text_file_example] // // create file item // #fname := set 'text_file_example.txt' #f := new file * '$#fname' /Text /Write if '$#f[?]' != 'file' then em -1 ; '$#mac - Create file failed ($EMSG)' end // // write data to file using 'write' command // write $#f 'The first line of this text file' /Newline if '$rc' > 0 em $rc ; '$#mac - Failed to write line to file' write $#f '%d:%d:%d' 1 2 30 /Format /Newline if '$rc' > 0 em $rc ; '$#mac - WRITE /Format failed ($EMSG)' // // write data to file using 'save' command // $#f save $(eval fill(10,0,1)) // // close file and reopen as read-only // delete $#f #f := new file * '$#fname' /Text /Read if '$#f[?]' != 'file' then em -1 ; '$#mac::Error - Create file failed ($EMSG)' end // // load data from file into a table and display the table // #t := new table * if '$#t[?]' != 'table' then em -1 ; '$#mac - Create table failed ($EMSG)' end $#f load $#t if '$rc' > 0 em $rc ; $#mac - LOAD failed ($EMSG) showitem $#t // // clean up // delete $#f $#t exit

© 2009 The Austrian Academy of Sciences Acoustics Research Institute