fopen
- open a file

File Input/Output functions
(AFL 2.5)


SYNTAX fopen( ''filename'', ''mode'', shared = False )
RETURNS FILE handle
FUNCTION Opens file, returns filehandle (NUMBER).

File handle is non-zero if file opened successfully, zero on failure.

Parameters:

  • filename - STRING - contains the path to the file name. Please note that single backslash in path must be written in AFL as (double backslash)
  • mode - STRING - access mode can be "r" - for reading, "w" for writing, "a" for appending (and all other regular C-runtime library modes)
  • shared (new in 5.80) - BOOLEAN - False - open file without checking for sharing, True - open file in share-aware mode.

When sharing parameter is True the following things happen:

  • when mode is "w" (writing) or "a" (appending) then file is open with a sharing flag that denies others to read and/or write at the same time
  • when mode is "r" (reading) then file is open with a sharing flag that denies others to write to the file (shared reads are allowed)
  • when open is unsuccessful due to sharing violation (other processes / threads have that file open and deny reads/writes) then fopen would wait for 250ms and retry automatically 4 times. After 4 unsuccessful retries it fails with file handle == Null.

When sharing parameter is set to False (default) no such checking occurs and file is open anyway without denying others to read/write. This may cause data corruption if file is written to from multiple threads/external processes at the same time. If you want to use this mode (sharing set to False) in multithreaded environment to write data, you need to care about synchronization yourself for example using critical section.

EXAMPLE fh = fopen( "myfile.txt", "w");
if( fh )
{
   
fputs( "Testing", fh );
}
else
{
   
printf("Error opening file");
}
SEE ALSO fclose() function , fputs() function , fgets() function

References:

The fopen function is used in the following formulas in AFL on-line library:

More information:

Updated on-line reference