Previous | Contents | Index |
When the UNIQUE option is used, the file is created with a unique name. These are usually temporary work or holding files for listings, etc. The temporary files by default are located in the Windows temporary folder for the current user. If you specify your own path for the file name, then that path will be used instead when creating the temporary file.
Example 14-6 Specifying Temporary File Location |
---|
open file temp_ch: name 'c:\sheerpower\somfolder\myfile.tmp', unique, access output ask #temp_ch: name filename$ print filename$ |
Example 14-7 UNIQUE option in OPEN#chnl_num |
---|
open #12: unique, access output ask #12: name x$ print x$ close #12 end c:\windows\temp\_fd56639b.tmp |
The following example illustrates how to create uniquely named files based on file_spec.
Example 14-8 UNIQUE option in OPEN#chnl_num |
---|
open #12: name 'payroll', unique, access output ask #12: name x$ print x$ close #12 end c:\windows\temp\payroll_fd5aefaf.tmp |
When writing to a file opened with the LOCK option, the file cannot be accessed by any other process. When a file is opened with LOCK, the following error message is returned:
Privilege or security level insufficient for operation |
Example 14-9 LOCK option in OPEN#chnl_num |
---|
open #1: name 'sheerpower:unformatted.txt', access output, unformatted, lock open #2: name 'sheerpower:unformatted.txt', access output end 18:25:50 Privilege or security level insufficient for operation at MAIN.0001 |
CLOSE [#chnl_num | ALL] |
Running the following example will create a file called 'test_file.txt' in your Sheerpower folder. |
Example 14-10 CLOSE#chnl_num statement |
---|
open #1: name 'test_file.txt', access output print #1: 'This is the first line.' print #1: 'Here is the second line.' close #1 end |
CLOSE #chnl_num closes a file. CLOSE ALL closes all files. Files should be closed before the program ends.
CLOSE closes a file from further access. Once a file is closed, data cannot be stored in it or read from it. chnl_num is the channel number associated with the file. (The channel number is assigned in the OPEN statement.) The channel number can be given as any numeric expression.
The PRINT #chnl_num statement writes data to a file so the data can be referenced at a later time.
PRINT #chnl_num [, USING print_mask]: [TAB(col){, | ;}] [expr {, | ;} [TAB(col){, | ;}] expr...] |
Example 14-11 Channel number in PRINT statement - PRINT#chnl_num |
---|
open #1: name 'test.txt', access output print #1, using '{ucase}?': 'first line' print #1: tab(5); 'second line' close #1 open #1: name 'test.txt' line input #1: record_1$, record_2$ print record_1$ print record_2$ close #1 end FIRST LINE second line |
PRINT writes data to a file. The file must be open and have a channel number associated with it. chnl_num is the channel number and it can be any numeric expression. expr is the expression being stored in the file. When Sheerpower executes a PRINT statement, it evaluates the expression and stores its value in the file. The expression is optional. A PRINT statement without an expression writes a blank line to the file.
See Section 6.7, File and Structure Access Functions for more on working with files in Sheerpower. |
14.4 INPUT #chnl_num: var, var...
INPUT #chnl_num: var, var... |
Example 14-12 Input to a file - INPUT#chnl_num statement |
---|
open #1: name 'number.ars', access output print #1: 1; 2; 3 close #1 open #1: name 'number.ars', access input input #1: a, b, c print a; b; c close #1 end 1 2 3 |
The INPUT #chnl_num statement is used to read the data stored in a file. The file must be open and have a channel number associated with it. The simplest version of the INPUT statement is:
INPUT #chnl_num: var |
chnl_num is the channel number associated with the file. The channel number can be any integer expression. var is the variable the data is assigned to. Each time data is input from a file, it must be assigned to a variable. The data input must match the variable's data type. Sheerpower inputs data sequentially starting at the beginning of the file.
14.4.1 Inputting multiple variables
One INPUT statement can be used to input data into a number of variables. The variables in the INPUT list must be separated by commas.
INPUT #chnl_num: var, var, var... |
Sheerpower inputs data sequentially, starting from the beginning of the file. Sheerpower continues inputting data until all the variables in the list have values.
Example 14-13 Inputting multiple variables |
---|
dim num(10) open #1: name 'number.ars', access output print #1: 1 print #1: 2 print #1: 3 close #1 open #1: name 'number.ars', access input for i = 1 to 3 input #1: num(i) print num(i); next i close #1 end 1 2 3 |
If the variable and data types do not match, an exception will be generated.
If an attempt is made to input more data than the file contains, an exception is generated.
14.5 LINE INPUT #chnl_num: str_var, str_var...
LINE INPUT #chnl_num [, EOF num_var]: str_var, str_var... |
Example 14-14 LINE INPUT#chnl_num statement |
---|
open #1: name 'test_file.txt', access input line input #1: line_1$, line_2$ print line_1$, line_2$ close #1 end This is the first line of text. This is the second line of text. |
LINE INPUT #chnl_num reads a line of text from a file. Everything on the line including commas, quotation marks, semicolons, etc., is accepted as part of the input line.
The file must be open and have a channel number associated with it. The simplest version of the LINE INPUT statement is:
LINE INPUT #chnl_num: str_var |
chnl_num is the channel number associated with the file. The channel number can be any integer expression. str_var is the variable to which data is being assigned. When Sheerpower executes the LINE INPUT statement, it reads a line from the file and assigns it to the string variable specified.
LINE INPUT accepts as the input data everything from the beginning of the line up to the first line terminator. The contents of the line---including commas, quotation marks, tabs, leading and trailing spaces, etc.---are assigned to the string variable specified. A string variable must be specified. If a numeric variable is specified, an error will result. If the line being read is empty, Sheerpower assigns a null string to the string variable.
Previous | Next | Contents | Index |