Skip to content

File Functions

File Functions

This is a group of functions for working with files.

For security reasons, work with files is strictly controlled in the MQL4 language. Files with which file operations are conducted using MQL4 means cannot be outside the file sandbox.

There are two directories (with subdirectories) in which working files can be located:

  • terminal_data_folder\MQL4\Files\ (in the terminal menu select to view “File” - “Open the data directory”);
  • the common folder for all the terminals installed on a computer - usually located in the directory C:\Documents and Settings\All Users\Application Data\MetaQuotes\Terminal\Common\Files.

There is a program method to obtain names of these catalogs using the TerminalInfoString() function, using the ENUM_TERMINAL_INFO_STRING enumeration:

//--- Folder that stores the terminal data
   string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
//--- Common folder for all client terminals
   string common_data_path=TerminalInfoString(TERMINAL_COMMONDATA_PATH);

Work with files from other directories is prohibited.

File functions allow working with so-called “named pipes”. To do this, simply call FileOpen() function with appropriate parameters.

FunctionAction
FileFindFirstStarts the search of files in a directory in accordance with the specified filter
FileFindNextContinues the search started by the FileFindFirst() function
FileFindCloseCloses search handle
FileOpenOpens a file with a specified name and flag
FileDeleteDeletes a specified file
FileFlushWrites to a disk all data remaining in the input/output file buffer
FileGetIntegerGets an integer property of a file
FileIsEndingDefines the end of a file in the process of reading
FileIsLineEndingDefines the end of a line in a text file in the process of reading
FileCloseCloses a previously opened file
FileIsExistChecks the existence of a file
FileCopyCopies the original file from a local or shared folder to another file
FileMoveMoves or renames a file
FileReadArrayReads arrays of any type except for string from the file of the BIN type
FileReadBoolReads from the file of the CSV type a string from the current position till a delimiter (or till the end of a text line) and converts the read string to a value of bool type
FileReadDatetimeReads from the file of the CSV type a string of one of the formats: “YYYY.MM.DD HH:MM:SS”, “YYYY.MM.DD” or “HH:MM:SS” - and converts it into a datetime value
FileReadDoubleReads a double value from the current position of the file pointer
FileReadFloatReads a float value from the current position of the file pointer
FileReadIntegerReads int, short or char value from the current position of the file pointer
FileReadLongReads a long type value from the current position of the file pointer
FileReadNumberReads from the file of the CSV type a string from the current position till a delimiter (or til the end of a text line) and converts the read string into double value
FileReadStringReads a string from the current position of a file pointer from a file
FileReadStructReads the contents from a binary file into a structure passed as a parameter, from the current position of the file pointer
FileSeekMoves the position of the file pointer by a specified number of bytes relative to the specified position
FileSizeReturns the size of a corresponding open file
FileTellReturns the current position of the file pointer of a corresponding open file
FileWriteWrites data to a file of CSV or TXT type
FileWriteArrayWrites arrays of any type except for string into a file of BIN type
FileWriteDoubleWrites value of the double type from the current position of a file pointer into a binary file
FileWriteFloatWrites value of the float type from the current position of a file pointer into a binary file
FileWriteIntegerWrites value of the int type from the current position of a file pointer into a binary file
FileWriteLongWrites value of the long type from the current position of a file pointer into a binary file
FileWriteStringWrites the value of a string parameter into a BIN or TXT file starting from the current position of the file pointer
FileWriteStructWrites the contents of a structure passed as a parameter into a binary file, starting from the current position of the file pointer
FolderCreateCreates a folder in the Files directory
FolderDeleteRemoves a selected directory. If the folder is not empty, then it can’t be removed
FolderCleanDeletes all files in the specified folder
FileOpenHistoryOpens file in the current history directory or in its subfolders

If the file is opened for writing using FileOpen(), all subfolders specified in the path will be created if there are no such ones.

Last updated on