|
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.
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.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.
To close a file, use the CLOSE statement:
close #file_handle
Example:
sheerpower provides several ways to read data from files.
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.
line input #file_handle, eof eof_flag?: variable$
TRUE
if the end of the file is reached; otherwise, it will
be FALSE
.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
statement.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.close #myfile
to free up resources.You can write data to a file using the 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!"
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:
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
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.