![]() |
|
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.
@
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.
You can also use logicals to represent file paths. When using a logical as a file path, the file specification takes the form:
logicalname:xxxx
Where logicalname
is the defined logical, and
xxxx
is the rest of the file path or filename. This approach
allows for flexibility in managing file paths, especially when directories
may change or need to be referenced dynamically.
For example:
MYFILES:data.txt
This would reference the data.txt
file located in the
directory represented by the logical MYFILES
.
To define a logical, for example, to point MYFILES to c:\users\myuser\myfiles
,
you would use the following code:
To work with a file, you first need to open it using the OPEN statement. You can open files for reading, writing, or appending.
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 use
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. Sheerpower ensures that file operations, such as reading and
writing, happen quickly and efficiently. By handling buffering and I/O operations behind the
scenes, developers don’t need to manage these complexities manually, which reduces the risk of errors.
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:
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. |