Jump to: navigation, search

Opens a file for reading or writing. After opening, files can be written to with WriteFile or read from with ReadFile. When you're done with the file, it should be closed with CloseFile to release its system resources.


Result = OpenFile(filename; mode);

Return Value

a handle to the file


filename string containing a valid path and filename
mode string containing the open mode (see below)

open modes

The mode parameter must be a string that begins with one of the following sequences (case sensitive!):

mode description starts..
r rb open for reading beginning
w wb open for writing (creates file if it doesn't exist). Deletes content and overwrites the file. beginning
a ab open for appending (creates file if it doesn't exist) end
r+ rb+ r+b open for reading and writing beginning
w+ wb+ w+b open for reading and writing. Deletes content and overwrites the file. beginning
a+ ab+ a+b open for reading and writing (append if file exists) end

The "b" stands for binary. The C standard provides for two kinds of files�text files and binary files�although operating systems are not required to distinguish between the two. A text file is a file consisting of text arranged in lines with some sort of distinguishing end-of-line character or sequence. When bytes are read in from a text file, an end-of-line sequence is usually mapped to a linefeed for ease in processing. When a text file is written to, a bare linefeed is mapped to the OS-specific end-of-line character sequence before writing. A binary file is a file where bytes are read in "raw", and delivered "raw", without any kind of mapping.

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. However, writes cannot be followed by reads without an intervening call to fflush or to a file positioning function (fseek, fsetpos, or rewind), and reads cannot be followed by writes without an intervening call to a file positioning function.[1]

Writing and appending modes will attempt to create a file of the given name, if no such file already exists. As mentioned above, if this operation fails, fopen will return NULL.


hFile             = OpenFile(filename; "wb");
WritePCRasterFile = WriteFile(hFile; MainHeader[]; MainHeaderFormat[]);
WritePCRasterFile = WritePCRasterFile + WriteFile(hFile; RasterHeader; RasterHeaderFormat);
WritePCRasterFile = WritePCRasterFile + WriteFile(hFile; BetweenHeaderAndData; "char");

See also