myeatrade

MQL4 Reference

MQL4 File functions (1)

A group of functions for working with files.

There are three directories (with subdirectories) where working files can be placed:

  • /HISTORY/<current broker> – especially for the FileOpenHistory function;
  • /EXPERTS/FILES – common case;
  • /TESTER/FILES – especially for testing.

Working with files from other directories is prohibited.

 

FileClose()

void FileClose(int handle)

Closes file previously opened by the FileOpen() function.

Parameters:

handle - File handle returned by the FileOpen() function.

Sample:

  int handle=FileOpen("filename", FILE_CSV|FILE_READ);
  if(handle>0)
    {
     // working with file ...
     FileClose(handle);
    }

FileDelete()

void FileDelete(string filename)

Removes specified file name. To get the detailed error information, call GetLastError().

Files can only be deleted if they are in the terminal_dir\experts\files directory (terminal_directory\tester\files, in case of testing) or its subdirectories.

Parameters:

filename - Path to the file.

Sample:

  // file my_table.csv will be deleted from terminal_dir\experts\files directory
  int lastError;
  FileDelete("my_table.csv");
  lastError=GetLastError();
  if(laseError!=ERR_NOERROR)
    {
     Print("An error ocurred while (",lastError,") deleting file my_table.csv");
     return(0);
    }

FileFlush()

void FileFlush(int handle)

Flushes all data stored in the file buffer to the disk.

Notes: The FileFlush() function must be called between operations of file reading and writing in the file.

At file closing, the data are flushed to the disk automatically, so there is no need to call the FileFlush() function before calling of the FileClose() function.

Parameters:

handle - File handle returned by the FileOpen() function.

Sample:

  int bars_count=Bars;
  int handle=FileOpen("mydat.csv",FILE_CSV|FILE_WRITE);
  if(handle>0)
    {
     FileWrite(handle, "#","OPEN","CLOSE","HIGH","LOW");
     for(int i=0;i<bars_count;i++)
       FileWrite(handle, i+1,Open[i],Close[i],High[i], Low[i]);
     FileFlush(handle);
     ...
     for(int i=0;i<bars_count;i++)
       FileWrite(handle, i+1,Open[i],Close[i],High[i], Low[i]);
     FileClose(handle);
    }

FileIsEnding()

bool FileIsEnding(int handle)

Returns logical true if file pointer is at the end of the file, otherwise returns false. To get the detailed error information, call GetLastError() function. If the file end is reached during reading, the GetLastError() function will return error ERR_END_OF_FILE (4099).

Parameters:

handle - File handle returned by the FileOpen() function.

Sample:

  if(FileIsEnding(h1))
    {
     FileClose(h1);
     return(false);
    }

FileIsLineEnding()

bool FileIsLineEnding(int handle)

For CSV file returns logical true if file pointer is at the end of the line, otherwise returns false. To get the detailed error information, call GetLastError() function.

Parameters:

handle - File handle returned by the FileOpen() function.

Sample:

  if(FileIsLineEnding(h1))
    {
     FileClose(h1);
     return(false);
    }

FileOpen()

int FileOpen(string filename, int mode, int delimiter=';')

Opens file for input and/or output. Returns a file handle for the opened file or -1 (if the function fails). To get the detailed error information, call GetLastError() function.

Notes: Files can only be opened in the terminal_directory\experts\files folder (terminal_directory\tester\files if for expert testing) or in its subfolders.

FILE_BIN and FILE_CSV modes cannot be used simultaneously.

If FILE_WRITE does not combine with FILE_READ, a zero-length file will be opened. If even the file containd some data, they will be deleted. If there is a need to add data to an existing file, it must be opened using combination of FILE_READ | FILE_WRITE.

If FILE_READ does not combine with FILE_WRITE, the file will be opened only if it already exists. If the file does not exist, it can be created using the FILE_WRITE mode.

No more than 32 files can be opened within an executable module simultaneously. Handles of files opened in the same module cannot be passed to other modules (libraries).

Parameters:

filename  - Filename. 
mode      - Opening mode. It can be one or combination of values: 
            FILE_BIN, FILE_CSV, FILE_READ, FILE_WRITE. 
delimiter - Delimiter character for csv files. 
            By default, the ';' symbol applies. 

Sample:

  int handle;
  handle=FileOpen("my_data.csv",FILE_CSV|FILE_READ,';');
  if(handle<1)
    {
     Print("File my_data.dat not found, the last error is ", GetLastError());
     return(false);
    }
This article url: http://www.myeatrade.com/443/

Please login to post comment, if you haven't account you can register free