Difference between revisions of "File.open"
Jump to navigation
Jump to search
(Update file mode descriptions for clarification) |
|||
Line 20: | Line 20: | ||
! Mode !! Description !! Starting Position | ! Mode !! Description !! Starting Position | ||
|- | |- | ||
− | |<tt>r</tt> || | + | |<tt>r</tt> || Opens an existing text file for reading. If the file does not exist, fopen() returns NULL. || beginning |
|- | |- | ||
− | |<tt>w</tt> || | + | |<tt>w</tt> || Opens a text file for writing. If the file exists, its contents are truncated (emptied). If the file does not exist, a new file is created. || beginning |
|- | |- | ||
− | |<tt>a</tt> || | + | |<tt>a</tt> || Opens a text file for appending. Data is written to the end of the file. If the file does not exist, a new file is created. || end |
|- | |- | ||
− | |<tt>r+</tt> || | + | |<tt>r+</tt> || Opens an existing text file for both reading and writing. The file must exist.|| beginning |
|- | |- | ||
− | |<tt>w+</tt> || | + | |<tt>w+</tt> || Opens a text file for both writing and reading. If the file exists, its contents are truncated. If the file does not exist, a new file is created. || beginning |
|- | |- | ||
− | |<tt>a+</tt> || | + | |<tt>a+</tt> || Opens a text file for both appending and reading. Data is written to the end of the file. If the file does not exist, a new file is created. || end |
|} | |} | ||
Revision as of 15:04, 29 September 2025
In Mini Micro and command-line MiniScript, the file.open
function returns a FileHandle object that enables further reads, writes, or updates of the file.
Arguments
Parameter Name | Default Value | Meaning |
---|---|---|
path | "" | full or partial path to the file of interest |
mode | "r+" | file mode (see below) |
File Modes
The second parameter to file.open
specifies whether the file is opened for reading, writing, or both.
Mode | Description | Starting Position |
---|---|---|
r | Opens an existing text file for reading. If the file does not exist, fopen() returns NULL. | beginning |
w | Opens a text file for writing. If the file exists, its contents are truncated (emptied). If the file does not exist, a new file is created. | beginning |
a | Opens a text file for appending. Data is written to the end of the file. If the file does not exist, a new file is created. | end |
r+ | Opens an existing text file for both reading and writing. The file must exist. | beginning |
w+ | Opens a text file for both writing and reading. If the file exists, its contents are truncated. If the file does not exist, a new file is created. | beginning |
a+ | Opens a text file for both appending and reading. Data is written to the end of the file. If the file does not exist, a new file is created. | end |
When a file is opened with update mode ( '+' as the second or third character in the mode argument), both input and output may be performed on the associated stream.
Writing and appending modes will attempt to create a file of the given name, if no such file already exists. If this operation fails, file.open
will return null
.
Example
This appends a new line to an existing text file (creating it if it does not already exist).
f = file.open("test.txt", "a")
f.write char(13) + "Hello world!"
f.close