Difference between revisions of "File.open"

From MiniScript Wiki
Jump to navigation Jump to search
(Created page with "In Mini Micro and command-line MiniScript, the <c>file.open</c> function returns a FileHandle object that enables further reads, writes, or updates of the file...")
 
 
Line 9: Line 9:
 
| path || "" || full or partial path to the file of interest
 
| path || "" || full or partial path to the file of interest
 
|-
 
|-
| mode || "rw+" || file mode (see below)
+
| mode || "r+" || file mode (see below)
 
|}
 
|}
  

Latest revision as of 13:46, 22 July 2024

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 open for reading (The file must exist) beginning
w open for writing (creates file if it doesn't exist). Deletes content and overwrites the file. beginning
a open for appending (creates file if it doesn't exist) end
r+ open for reading and writing (The file must exist) beginning
w+ open for reading and writing. If file exists deletes content and overwrites the file, otherwise creates an empty new file beginning
a+ open for reading and writing (append if file exists) 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