OpenFile()

Syntax

Result = OpenFile(#File, Filename$ [, Flags])
Description
Opens a file for reading/writing or creates a new file if it does not exist.

Parameters

#File The number to identify the file. #PB_Any can be used to auto-generate this number.
Filename$ The filename and path to the file. If the filename does not include a full path, it is interpreted relative to the current directory.
Flags (optional) It can be a combination (using the '| operand) of the following values:
  #PB_File_SharedRead : the opened file can be read by another process (Windows only).
  #PB_File_SharedWrite: the opened file can be written by another process (Windows only).
  #PB_File_Append     : the file pointer position will be set at the end of file.
  #PB_File_NoBuffering: the internal PureBasic file buffering system will be disabled for this file.
                        FileBuffersSize() can not be used on this file.
combined with one of the following values (the following flags affect the WriteString(), WriteStringN(), ReadString(), ReadCharacter() and WriteCharacter() behaviour):
  #PB_Ascii  : all read/write string operation will use ASCII if not specified otherwise.
  #PB_UTF8   : all read/write string operation will use UTF-8 if not specified otherwise (default).
  #PB_Unicode: all read/write string operation will use Unicode if not specified otherwise.

Return value

Returns nonzero if the file was opened successfully and zero if there was an error. If #PB_Any was used as the #File parameter then the new generated number is returned on success.

Remarks

This function fails if the file cannot be opened with write permission, for example if the file is located on a read-only file-system like a CD. To open a file for reading only, use the ReadFile() function. To overwrite an existing file with a new and empty file, use the CreateFile() function.

The file pointer will be positioned at the beginning of the file. To append data to the end of the file, use the #PB_File_Append flag to set the pointer to the end of the file.

Example


  If OpenFile(0, "Test.txt")    ; opens an existing file or creates one, if it does not exist yet
    FileSeek(0, Lof(0))         ; jump to the end of the file (result of Lof() is used)
    WriteStringN(0, "... another line at the end.")
    CloseFile(0)
  EndIf

See Also

CreateFile(), ReadFile(), CloseFile()

Supported OS

All

<- Lof() - File Index - ReadAsciiCharacter() ->