SheerPower® 4GL
A Guide to the SheerPower Language


Previous Contents Index

14.1.3.2 UNFORMATTED Option

When writing to a file opened as UNFORMATTED, the writes are done without any carriage control. This allows various character sequences to be sent to the channel without having CR/LF (carriage return/line feed) sequences sent as well.

Important note for the following example:

This example will create a NEW file in your SheerPower folder called 'unformatted.txt'. To view the result of this example, you need to open and view 'unformatted.txt' in SPDEV after you run the program. See Section 1.2, Creating a New Program in SheerPower for instructions on opening files in SPDEV.

Example 14-3 UNFORMATTED option in OPEN#chnl_num

  open #1: name 'sheerpower:unformatted.txt', access output, unformatted
  for i = 1 to 10 
    print #1: i; 
  next i 
  print #1: 
  end
  
 
 1  2  3  4  5  6  7  8  9  10 

Read and Write Binary Files Using the UNFORMATTED Option

SheerPower reads and writes binary files using the UNFORMATTED option of the OPEN statement. When reading unformatted data, up to 40960 bytes are read at a time. The following example gets a chunk of binary data from a file, and then takes the first 121 bytes of that file and stores it into a variable.

Example 14-4 Read/Write Binary Files using UNFORMATTED

  open file myfile: name 'somefile.xxx', unformatted 
  line input #myfile: rec$  // read a "chunk" of binary data 
  firstbytes$ = left(rec$, 121)  // Just the first 121 bytes 
  //Where 'somefile.xxx' is the name of your binary file. 

14.1.3.3 OPTIMIZE OFF Option

When OPTIMIZE OFF is specified, the file is opened without any of the SheerPower I/O optimizations.

Example 14-5 OPTIMIZE OFF option in OPEN#chnl_num

  open #2: name 'report.txt', optimize off, access output
  end 

14.1.3.4 UNIQUE Option

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 

14.1.3.5 LOCK Option

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 

14.2 CLOSE #chnl_num

FORMAT:


        CLOSE [#chnl_num | ALL] 

Important note for the following example:

Running the following example will create a file called 'test_file.txt' in your SheerPower folder.

EXAMPLE:

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

PURPOSE:

CLOSE #chnl_num closes a file. CLOSE ALL closes all files. Files should be closed before the program ends.

DESCRIPTION:

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.

14.3 PRINT #chnl_num

The PRINT #chnl_num statement writes data to a file so the data can be referenced at a later time.

FORMAT:


        PRINT #chnl_num [, USING print_mask]: [TAB(col){, | ;}] [expr {, | ;} 
                                              [TAB(col){, | ;}] expr...] 

EXAMPLE:

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 

DESCRIPTION:

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.

Note

See Section 6.7, File and Structure Access Functions for more on working with files in SheerPower.

14.4 INPUT #chnl_num: var, var...

FORMAT:


        INPUT #chnl_num: var, var... 

EXAMPLE:

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 

DESCRIPTION:

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.


Previous Next Contents Index