Cicode Programming Reference > Cicode Function Categories > File Functions Introduction > FileOpen

FileOpen

Opens a file and returns a file number that can be used by other file functions. The maximum file size supported is 1 Megabyte for displaying text files.

You can also use this function to check if a file exists, by opening the file in read-only mode. A return value of -1 indicates that the file cannot be opened.

ErrSet(1) needs to be in the previous line of your code, else the execution stops and a hardware error is generated. If ErrSet(1) is used then it doesn't halt, and -1 is returned.

Syntax

FileOpen(Name, Mode)

Name:

The name of the file to open.

Mode:

The mode of the opened file:

"a" - Append mode. Allows you to append to the file without removing the end of file marker. The file cannot be read. If the file does not exist, it will be created.

"a+" - Append and read modes. Allows you to append to the file and read from it. The end of file marker will be removed before writing and restored when writing is complete. If the file does not exist, it will be created.

"r" - Read-only mode. Allows you to (only) read from the file. If the file does not exist or cannot be found, the function call will return the value -1.

"r+" - Read/write mode. Allows you to read from, and write to, the file. If the file already exists (before the function is called), its contents will be deleted. If the file does not exist or cannot be found, the function call will return the value -1.

"w" - Write mode. Opens an empty file for writing. If the file already exists (before the function is called), its contents will be deleted. If the file does not exist or cannot be found, the file will be created.

"w+" - Read/write mode. Opens an empty file for both reading and writing. If the file already exists (before the function is called), its contents will be deleted. If the file does not exist or cannot be found, the file will be created.

Return Value

The file number. If the file cannot be opened, -1 is returned and the code is halted.

Related Functions

FileClose, FileRead, FileReadLn, FileWrite, FileWriteLn

Example

! Open a file in read-only mode.
ErrSet(1);
File=FileOpen("C:\Data\Report.Txt","r");
ErrSet(0);

See Also

File Functions