/
fileOpen

fileOpen

Description

Opens a new file for reading and/or writing.

Use this command as the first step in your custom File I/O scripts.

The file must first be opened before you can read or write to it.

If you are going to be reading a file that already exists on the file system, specify Read mode (using "r" as the second argument). Read mode will not allow you to change the contents of the file (i.e. no writeXXXX commands will be allowed). Read mode is typically used when writing your own file importers.

If you are planning on writing to a file on the file system, use either Write mode (with "w" as the second argument) or Append mode (with "a" as the second argument).

Write mode always erases any existing data in the file and starts writing from the beginning of the file. Use Write mode when writing your own file exporters.

Append mode doesn't touch existing file data and allows you to append data to the end of the file only. Use Append mode when writing to a cumulative log file, for example.

Note that the file you are trying to open must have proper permissions for the type of operation you are doing, otherwise the command will fail. Be sure to close the file using the fileClose command when your script is finished.

Functional area

Disk I/O

Command syntax

Syntax

fileOpen "fileName" "openMode"[-b]

Arguments

NameTypeRequiredComments
openModestring
Can be r (Read), w (Write), or a (Append) mode. See Description above for information on each mode.
fileNamestring
Full path to a file in your file system (eg c:/temp/myfile.txt)

Flags

NameFlag argumentsArgument typeExclusive toComments
b0

Return value

integer

Examples

int $fileID;
string $fileName = "c:/test.txt"; 
// Open the file and get the file ID, so we can write to the file
// later
$fileID = `fileOpen $fileName "w"`;
 
// Write a string to the file, passing in the file ID
// as the first argument. Almost all File I/O commands
// take a file ID as the first argument.
writeString $fileID "This is some text";
 
// Be sure to close the file after you are done
fileClose $fileID;

Additional information

Related commands