Skip to content

Opening files in append mode doesn't work as documented #58

@MineRobber9000

Description

@MineRobber9000

According to the MiniScript wiki:

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, it 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

Notably, opening a nonexistent file in append mode does not, in fact, create it. Instead, the file is left in a sort of limbo state; writing any content to it will cause it to spontaneously exist, but if you don't write anything to it, the file will never be created. Compare opening a nonexistent file in write mode, which creates the file as soon as the handle is closed.

This wouldn't be too much of an issue if you could just write an empty string, but FileHandle.write doesn't let you do that, meaning that the only way to do "create an empty file if the file doesn't already exist" is to check for if the file exists and file.writeLines filename, [] if it doesn't.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions