Sheerpower®
A Guide to the Sheerpower Language


Previous Contents Index


Chapter 14
File Handling

Files are places where information is stored. Files can be accessed from within Sheerpower programs, but files exist outside of the program. Therefore, when a program ends, the file and the information in it still exists. The next time the program is run, the file can be accessed and old information removed from it, and new information stored in it.

Files are stored on devices: disks, drives, etc. They are stored under file specifications, which include a device name.

The following pages describe the Sheerpower statements used to manipulate files.

Note

See Section 6.7, File and Structure Access Functions for more on working with files in Sheerpower. See Section 6.7.5, FINDFILE$(str_expr [, int_expr]) to process a series of files, or batch files.

14.1 OPEN FILE var_name: NAME ...

FORMAT:


        OPEN FILE var_name: NAME 'file_spec' 
               [, ACCESS INPUT | OUTPUT [,SHARE] | OUTIN [,SHARE] ] [,UNFORMATTED] 
               [, UNIQUE] [, OPTIMIZE OFF] [, LOCKED] 

EXAMPLE:

Example 14-1 OPEN FILE Statement

  open file text_ch: name 'test_file.txt', access output, share
  print #text_ch: 'This is the first line of text.' 
  print #text_ch: 'This is the second line of text.' 
  close #text_ch 
  
  open file text_ch: name 'test_file.txt' 
  line input #text_ch: line_1$ 
  line input #text_ch: line_2$ 
  print line_1$ 
  print line_2$ 
  close #text_ch 
  
 
This is the first line of text. 
This is the second line of text. 

PURPOSE:

The OPEN FILE statement is used to open existing files or create new files. You can also access webpage data using the HTML:// FILE OPEN option, or send emails with the MAILTO:// FILE OPEN option. See Chapter 18, Writing Network Applications and Accessing Devices for more details.

DESCRIPTION:

OPEN FILE either opens an existing file or creates a new one if the file does not already exist. The OPEN FILE syntax looks up a free "channel", stores the channel into var_name, and then opens the file.

file_spec gives the file specification of the file being opened. The file specification can be any string expression.

14.1.1 OPEN #chnl_num: NAME ...

FORMAT:


        OPEN #chnl_num: NAME 'file_spec' 
               [, ACCESS INPUT | OUTPUT [,SHARE] | OUTIN [,SHARE] ] [, UNFORMATTED] 
               [, UNIQUE] [, OPTIMIZE OFF] [, LOCKED] 

EXAMPLE:

Example 14-2 OPEN#chnl_num Statement

  open  #1: name 'test_file.txt', access output
  print #1: 'This is the first line of text.' 
  print #1: 'This is the second line of text.' 
  close #1 
  
  open  #1: name 'test_file.txt' 
  line input #1: line_1$, line_2$ 
  print line_1$ 
  print line_2$ 
  close #1 
 
 
This is the first line of text. 
This is the second line of text. 

PURPOSE:

OPEN #chnl_num: NAME works the same as Section 14.1, OPEN FILE var_name: NAME .... The statement opens a file so it can be read and written to. If the file does not exist it is created. The preferred method to open or create files is to use Section 14.1, OPEN FILE var_name: NAME ....

DESCRIPTION:

OPEN either opens an existing file or creates a new one. #chnl_num is the channel number associated with the file. chnl_num can be any integer number in the range of 1 to 95. (0 is the channel number associated with the terminal. Channel number 0 cannot be opened or closed.) The channel number is used to refer to the file. The channel number must be unique. If a channel number is already associated with an open file, an exception is generated.

file_spec gives the file specification of the file being opened. The file specification can be any string expression.

14.1.2 Logicals

In Sheerpower, the @ sign is a LOGICAL that is translated from being just an "@" to the full PATH of the folder that the current application is being run from. For example:


  open #1: name '@filename', access outin 

sheerpower: is also a logical. If the file to be opened is inside the Sheerpower folder, it can be opened as:


  open #1: name 'sheerpower:filename', access outin 

You can also open a file using the full path of where the file is located. For example:


  open #1: name 'c:\foldername\filename', access outin 

In Sheerpower you can make your own logicals. See Section 11.15.7, SET SYSTEM, LOGICAL: VALUE.

14.1.3 OPEN Options

The OPEN statement has several options. Multiple options are separated with commas.

14.1.3.1 ACCESS Option

The ACCESS option specifies one of three input/output options. These options tell Sheerpower whether you want to input (read) data, output (store) data or input and output data.

ACCESS INPUT

ACCESS OUTPUT

ACCESS OUTIN

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 


Previous Next Contents Index