Sheerpower Logo
B.6  Working with files

sheerpower File Handling

sheerpower's file handling capabilities are versatile and robust, allowing you to work with files in various ways. Here’s a detailed overview of how to work with files in sheerpower, based on the documentation found here.

1. Opening and Closing Files

To work with a file, you first need to open it using the OPEN statement. You can open files for reading, writing, or appending.

Note: In sheerpower, when specifying file paths, the '@' symbol at the beginning of a path refers to the directory of the currently running program. This makes it easy to reference files relative to your program's location, enhancing portability and simplifying path management.
open file file_handle: name filename$, access access_mode
  • file_handle: A variable that represents the file.
  • filename$: The name of the file to be accessed.
  • access_mode: The mode in which the file is opened (e.g., input, output, append, update).

The file can also be opened in shared mode by appending share to the access_mode. This allows other processes to access the file while the program is running and writing to it. Shared mode is especially useful for log files that may need to be viewed while the program is still running.

open file myfile: name 'data.txt', access input open file myfile: name 'data.txt', access output open file myfile: name 'data.txt', access output, shared open file myfile: name 'data.txt', access append open file myfile: name 'data.txt', access append. shared open file myfile: name 'data.txt', access update open file myfile: name 'data.txt', access update, shared

To close a file, use the CLOSE statement:

close #file_handle

Example:

close #myfile

Reading from Files

sheerpower provides several ways to read data from files.

Reading from Files Using line input

The line input statement in SheerPower is used to read a file one line at a time. This means that each time you call line input, it reads the next line of text from the file and stores it in a string variable. It also allows you to check for the end-of-file (EOF) condition to determine when there are no more lines to read.

Syntax:

line input #file_handle, eof eof_flag?: variable$

Explanation:

  • #file_handle: The handle of the file you have opened for reading.
  • eof eof_flag?: This optional clause checks whether the end of the file has been reached.
    • eof_flag?: A boolean variable that will be set to TRUE if the end of the file is reached; otherwise, it will be FALSE.
  • variable$: The string variable where the line of text will be stored.

Example:

Here is an example that demonstrates how to read a file one line at a time until the end of the file is reached:

open file myfile: name 'myfile.txt' do line input #myfile, eof eof_flag?: line$ if eof_flag? then exit do print line$ loop close #myfile

Steps Explained:

  1. Opening the File: The file is opened for input using the open file statement.
  2. Reading the File One Line at a Time: Inside the do loop, line input reads the next line of text from the file into line$. The eof eof_flag? clause checks if the end of the file has been reached after reading the line. If eof_flag? is TRUE, the loop exits to prevent trying to read past the end of the file.
  3. Processing Each Line: Each line read from the file is printed out (or can be processed as needed).
  4. Closing the File: The file is closed with close #myfile to free up resources.

Writing to Files

You can write data to a file using the PRINT statement.

PRINT Statement

The PRINT statement writes text to a file, followed by a newline character. Anything that can be written to the console can also be written to a file. Writing to a file is very fast because SheerPower automatically handles all I/O buffering and write-behind operations for the developer. In the case of Shared access, SheerPower also periodically flushes the file buffers to disk, ensuring that other processes can read a fresh copy of the data.

print #file_handle: data$

Example:

print #myfile: "Hello, World!"

Updating Files

When you need to update an existing file, you can open it in update mode. This allows you to read from and write to the file.

Example:

open file myfile: name 'data.txt', access update line input #myfile: line$ print #myfile: "Updated line" close #myfile

Handling File Errors

To handle errors that might occur during file operations, use the WHEN EXCEPTION IN...USE construct.

Example:

when exception in
  open file myfile: name 'data.txt', access input
use
  print "Error opening file"
end when

Example Program: Reading and Writing a File

Here's an example program that demonstrates reading from and writing to a file:

! Open a file for writing
open file myfile: name 'example.txt', access output

! Write data to the file
print #myfile: "This is a test file."
print #myfile: "It contains several lines of text."

! Close the file
close #myfile

! Open the file for reading
open file myfile: name 'example.txt', access input

! Read and display the content of the file
do
  line input #myfile, eof eof_flag?: line$
  if eof_flag? then exit do
  print line$
loop

! Close the file
close #myfile

This program writes two lines of text to a file and then reads and displays the content of the file.

For more detailed information and advanced file handling features in sheerpower, refer to the official sheerpower Documentation.

```
Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.
Wide screen