File management functions

The following functions enable sequential read / write operations in disk files:

F_ROPEN    open a file for reading
F_WOPEN   
create or reset a file and open it for writing
F_AOPEN   
create or open a file in append mode
F_CLOSE   
close an open file
F_EOF     
test if the end of file is reached in a file open for read
FA_READ   
read a DINT integer from a binary file
FA_WRITE  
write a DINT integer to a binary file
FM_READ   
read a STRING value from a text file
FM_WRITE  
write a STRING value to a text file
FB_READ    read binary data from a file
FB_WRITE   write binary data to a file
F_EXIST    test if a file exist
F_GETSIZE  get the size of a file
F_COPY     copy a file
F_DELETE   remove a file
F_RENAME   rename a file

Related function blocks:

LogFileCSV  log values of variables to a CSV file

Each file is identified in the application by a unique handle manipulated as a DINT value. The file handles are allocated by the target system. Handles are returned by the Open functions and used in all other calls for identifying the file.

Important notes

  • File are opened and closed directly by the Operating System of the target. Opening some files may be dangerous for system safety and integrity. The number of open files may be limited by the target system.
  • Opening a file may be unsuccessful (invalid path or file name, too many open files...) Your application should process such error cases in a safe way.

  • File management may be not available on some targets. Please refer to OEM instructions for further details about available features.

  • Valid paths for storing files depend on the target implementation. Please refer to OEM instructions for further details about available paths.

  • Using the simulator, all pathnames are ignored, and files are stored in a reserved directory. Only the file name passed to the Open functions is taken into account.

F_ROPEN: open a file for reading

ID := F_ROPEN (PATH);

PATH : STRING name of the file - the file must exist
             
may include a path name according to target system conventions.
ID : DINT    
ID of the open file or NULL if the file cant be read

F_WOPEN: open a file for writing

ID := F_WOPEN (PATH);

PATH : STRING name of the file
             
may include a path name according to target system conventions.
ID : DINT    
ID of the open file or NULL if the file cant be open

If the file does not exist, it is created. If the file already exists, its contents is cleared.

F_AOPEN: open a file in "append" mode

ID := F_WOPEN (PATH);

PATH : STRING name of the file
             
may include a pathname according to target system conventions.
ID : DINT    
ID of the open file or NULL if the file cant be open

If the file does not exist, it is created. If the file already exists, it is open at the end for append.

F_CLOSE: close an open file

OK := F_CLOSE (ID);

ID : DINT     ID of the open file
OK : BOOL     return check: TRUE if successful

F_EOF: test if end of file is encountered

OK := F_EOF (ID);

ID : DINT     ID of a file open for reading
OK : BOOL     TRUE if the end of file has been encountered

F_EOF must be used only for files open in read mode by the F_ROPEN  function.

FA_READ: read a DINT value from a file

Q := FA_READ (ID);

ID : DINT     ID of a file open for reading
Q : DINT      read value or 0 in case or error

Integer values read by FA_READ must have been written by the FA_WRITE function. Integers are stored in binary format in the file, using memory conventions of the target system.

FA_WRITE: write a DINT value to a file

OK := FA_WRITE (ID, IN);

ID : DINT     ID of a file open for writing
IN : DINT     integer value to be written
OK : BOOL    
return check: TRUE if successful

Integers are stored in binary format in the file, using memory conventions of the target system.

FM_READ: read a STRING value from a file

Q := FM_READ (ID);

ID : DINT     ID of a file open for reading
Q : STRING    read value or empty string in case or error

This function is intended to read a text line in the file. Reading stops when end of line character is encountered. Reading stops when the maximum length declared for the return variable is reached.

FM_WRITE: write a STRING value to a file

OK := FM_WRITE (ID, IN);

ID : DINT     ID of a file open for writing
IN : STRING   string value to be written
OK : BOOL    
return check: TRUE if successful

This function writes a text line in the file. End of line character is systematically written after the input string.

FB_READ: read binary data from a file

OK := FB_READ (ID, V);

ID : DINT     ID of a file open for reading
V : ANY       variable to be read (cannot be string)
OK : BOOL     return check: TRUE if successful

Variables are stored in binary format in the file, using memory conventions of the target system.

FB_WRITE: write binary data to a file

OK := FB_WRITE (ID, V);

ID : DINT     ID of a file open for writing
V : ANY       variable to be written (cannot be string)
OK : BOOL    
return check: TRUE if successful

Variables are stored in binary format in the file, using memory conventions of the target system.

F_EXIST: test if file exist

OK := F_EXIST (PATH);

PATH : STRING name of the file
             
may include a pathname according to target system conventions.
OK : BOOL    
TRUE if the file exists

F_GETSIZE: get file size

SIZE := F_GETSIZE (PATH);

PATH : STRING name of the file
             
may include a pathname according to target system conventions.
SIZE : DINT  
Size of the file in bytes

F_COPY: copy a file

OK := F_COPY (SRC, DST);

SRC : STRING  name of the source file (must exist)
             
may include a pathname according to target system conventions.
DST : STRING 
name of the destination file
             
may include a pathname according to target system conventions.
OK : BOOL    
TRUE if successful

F_DELETE: remove a file

OK := F_DELETE (PATH);

PATH : STRING name of the file (must exist)
             
may include a pathname according to target system conventions.
OK : BOOL    
TRUE if successful

F_RENAME: rename a file

OK := F_RENAME (PATH, NEWNAME);

PATH : STRING    name of the file (must exist)
                
may include a pathname according to target system conventions.
NEWNAME : STRING
new name for the file
OK : BOOL       
TRUE if successful