Name
OpenFile -- open a file for reading and writing
Synopsis
[id] = OpenFile(id, filename$[, mode, table])
Function
This function attempts to open the file specified by filename$ and assigns id to it. If you pass Nil in id, OpenFile() will automatically choose a vacant identifier and return it. If the file does not exist, this function will fail unless you use the mode argument to open a file for writing. In that case, OpenFile() will create the file for you.

All read and write operations will start at the current file cursor position. You can manually set the file cursor by using the Seek() function but it is also increased if you use other functions which read from or write to the file.

Starting with Hollywood 2.0 you can use the optional argument mode to open the file in read (default) or write mode or in shared mode, which means that you can read from the file and you can also write to it. If a file is opened in read mode, all write operations will fail. If a file is opened in write mode, all read operations will fail.

Starting with Hollywood 6.0 this function accepts an optional table argument which can be used to pass additional parameters. The following table elements are currently recognized:

Adapter:
This tag allows you to specify one or more file adapters that should be asked to open the specified file. This must be set to a string containing the name(s) of one or more adapter(s). Defaults to default. See Loaders and adapters for details. (V6.0)

Although Hollywood will automatically close all open files when it quits, it is strongly advised that you close an open file when you are done with it using the CloseFile() function so that it becomes available to the operating system again.

This command is also available from the preprocessor: Use @FILE to preopen files!

Inputs
id
identifier of the file or Nil for auto id selection
filename$
name of the file to open
mode
mode to open the file; can be #MODE_READ, #MODE_WRITE or #MODE_READWRITE (defaults to #MODE_READ) (V2.0)
table
optional: table containing further parameters (V6.0)
Results
id
optional: identifier of the file; will only be returned when you pass Nil as argument 1 (see above)
Example
OpenFile(1, "Highscores.txt")
While Not Eof(1) Do NPrint(ReadLine(1))
CloseFile(1)
This code opens the file "Highscores.txt" as file 1 and prints all of its lines to the screen.

Show TOC