MQL4参考 文件函数 FileOpen


The function opens the file with the specified name and flag.

int  FileOpen(
   string  file_name,           // File name
   int     open_flags,          // Combination of flags
   short   delimiter=';',       // Delimiter
   uint    codepage=CP_ACP      // Code page



[in]  The name of the file can contain subfolders. If the file is opened for writing, these subfolders will be created if there are no such ones.


[in] combination of flags determining the operation mode for the file. The flags are defined as follows:
FILE_READ file is opened for reading
FILE_WRITE file is opened for writing
FILE_BIN binary read-write mode (no conversion from a string and to a string)
FILE_CSV file of csv type (all recorded items are converted to the strings of unicode or ansi type, and are separated by a delimiter)
FILE_TXT a simple text file (the same as csv, but the delimiter is not taken into account)
FILE_ANSI lines of ANSI type (single-byte symbols)
FILE_UNICODE lines of UNICODE type (double-byte characters)
FILE_SHARE_READ shared reading from several programs
FILE_SHARE_WRITE shared writing from several programs
FILE_COMMON location of the file in a shared folder for all client terminals \Terminal\Common\Files


[in]  value to be used as a separator in txt or csv-file. If the csv-file delimiter is not specified, the default delimiter is ";". If the txt-file delimiter is not specified, then no separator is used. If the separator is clearly set to 0, then no separator is used.


[in]  Optional parameter. The value of the code page. For the most-used code pages provide appropriate constants.

Return Value

If a file has been opened successfully, the function returns the file handle, which is then used to access the file data. In case of failure returns INVALID_HANDLE.


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.

The file is opened in the folder of the client terminal in the subfolder MQL4\files (or Tester\Files in case of testing). If FILE_COMMON is specified among flags, the file is opened in a shared folder for all MetaTrader 4 client terminals.

"Named pipes" can be opened according to the following rules:

  • Pipe name is a string, which should have the following look: "\\servername\pipe\pipename", where servername - server name in the network, while pipename is a pipe name. If the pipes are used on the same computer, the server name can be omitted but a point should be inserted instead of it: "\\.\pipe\pipename". A client trying to connect the pipe should know its name.
  • FileFlush() and FileSeek() should be called to the beginning of a file between sequential operations of reading from the pipe and writing to it.

A special symbol '\' is used in shown strings. Therefore, '\' should be doubled when writing a name in MQL4 application. It means that the above example should have the following look in the code: "\\\\servername\\pipe\\pipename".

More information about working with named pipes can be found in the article "Communicating With MetaTrader 5 Using Named Pipes Without Using DLLs".


//| Script program start function                                    |
void OnStart()
//--- incorrect file opening method
   string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
   string filename=terminal_data_path+"\\MQL4\\Files\\"+"fractals.csv";
   int filehandle=FileOpen(filename,FILE_WRITE|FILE_CSV);
      Print("Failed to open the file by the absolute path ");
      Print("Error code ",GetLastError());
//--- correct way of working in the "file sandbox"
      FileWrite(filehandle,TimeCurrent(),Symbol(), EnumToString(ENUM_TIMEFRAMES(_Period)));
      Print("FileOpen OK");
   else Print("Operation FileOpen failed, error ",GetLastError());
//--- another example with the creation of an enclosed directory in MQL4\Files\
   string subfolder="Research";
      FileWrite(filehandle,TimeCurrent(),Symbol(), EnumToString(ENUM_TIMEFRAMES(_Period)));
      Print("The file most be created in the folder "+terminal_data_path+"\\"+subfolder);
   else Print("File open failed, error ",GetLastError());

See also

Use of a Codepage, FileFindFirst(), FolderCreate(), File opening flags