![]() |
Working with files |
Nearly every application needs to save or read information. In Sheerpower, this is done by working with files. This tutorial will walk you through the entire lifecycle of a file: creating it, writing to it, and reading it back, all while highlighting Sheerpower's efficient features.
Before you can work with a file, you must first
open it. The OPEN
statement establishes a
connection and gives you a file handle—a
temporary nickname, always starting with #
, that you
will use to refer to that file.
When you open a file, you must tell Sheerpower how you want to use it. This is called the access mode.
shared
ideal for
log files that are being watched as a program runs.
Problem: In many applications, log files don’t display new entries until the buffer fills or the file is closed, making real-time monitoring unreliable.
Solution: Sheerpower’s shared
access
mode automatically flushes writes often enough that other processes can
see updates almost immediately—without requiring extra code.
Efficiency: Instead of flushing on every write (which wastes I/O), Sheerpower uses a timed cycle, balancing responsiveness with performance.
Takeaway: Use shared
mode when you need
log files or outputs that remain live and visible as your program runs,
with no extra effort.
Let's walk through the full process of creating a simple settings file.
We'll OPEN
a new file for `output` and use
PRINT #
to write lines of text into it.
Sheerpower simplifies a common task: writing the exact same information
to multiple places simultaneously. Instead of writing separate
PRINT
statements for each destination, you can list all the
file handles in a single, efficient command.
A frequent use case is logging an event to both the user's screen (the console) and a permanent log file. Sheerpower makes this trivial.
When you run this code, the message
INFO: System backup completed successfully.
will appear on
your screen and will be added to the end of the activity.log
file.
Problem: When you need to send the same output to
multiple places (like console and log files), writing separate
PRINT
statements clutters your code and risks mistakes.
Solution: Sheerpower allows you to list multiple
file handles in a single PRINT
statement, ensuring the
exact same content is written everywhere at once.
Efficiency: This approach is cleaner and less error-prone, reducing duplication while making your intent obvious at a glance.
Takeaway: Use multi-handle printing whenever you need to mirror the same message to several destinations—it keeps your code concise, reliable, and easy to maintain.
You should always
CLOSE
a file when you are done with it. This saves your
changes to the disk. Any open files are automaically closed
when the progrm ends.
Now we can reopen the file for `input` to read the data back. We use
a DO/LOOP
and the line input
statement to
read the file one line at a time until we reach the end.
name '@data.txt'
). The `@` is a
special character that always refers to the directory where your
program is currently running. This ensures your code can find its
files no matter where you move it.
What happens if you try to open a file for `input` that doesn't
exist? Your program will stop with an error. To prevent this, you
can wrap your code in a WHEN EXCEPTION IN
block to
catch potential errors and handle them gracefully.
OPEN
a file with the correct
access mode to get a handle.PRINT #
to write or
line input #
to read.CLOSE
the file when you are
finished.WHEN EXCEPTION IN
to handle
potential errors safely.OPEN
to create a file handle (prefix #
)
for working with a file.output
(overwrite), append
(add to end), input
(read-only), update
(read/write), and shared
(timed flush for log files).PRINT #
, read with line input #
.PRINT
statement.CLOSE
files when finished to save data
and release resources.@
in file paths for portability across
directories.WHEN EXCEPTION IN
to handle missing or
inaccessible files gracefully.
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. |