myeatrade

MQL4 Reference

MQL4 File functions (2)

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.

 

FileOpenHistory()

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

Opens file in the current history directory (terminal_directory\history\server_name) or in its subfolders. Returns the file handle for the opened file. If the function fails, the returned value is -1. To get the detailed error information, call the GetLastError() function.

Notes: Client terminal can connect to servers of different brokerage companies. History data (HST files) for each brokerage company are stored in the corresponding subfolder of the terminal_directory\history folder.

The function can be useful to form own history data for a non-standard symbol and/or period. The file formed in the history folder can be opened offline, not data pumping is needed to chart it.

Parameters:

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

Sample:

  int handle=FileOpenHistory("USDX240.HST",FILE_BIN|FILE_WRITE);
  if(handle<1)
    {
     Print("Cannot create file USDX240.HST");
     return(false);
    }
  // work with file
  // ...
  FileClose(handle);

FileReadArray()

int FileReadArray(int handle, object&array[], int start, int count)

Reads the specified amount of elements from the binary file into array. Before reading, make sure that the array is large enough. Returns the amount of actually read elements.

To get the detailed error information, call the GetLastError() function.

Parameters:

handle  - File handle returned by the FileOpen() function. 
array[] - Array where data will be stored. 
start   - Starting position for storing in array. 
count   - Count of elements to be read. 

Sample:

  int handle;
  double varray[10];
  handle=FileOpen("filename.dat", FILE_BIN|FILE_READ);
  if(handle>0)
    {
     FileReadArray(handle, varray, 0, 10);
     FileClose(handle);
    }

FileReadDouble()

double FileReadDouble(int handle, int size=DOUBLE_VALUE)

Reads the double-precision number with floating point from the current binary file position. The number format size can be 8 bytes (double) or 4 bytes (float).

To get the error information, one has to call the GetLastError() function.

Parameters:

handle - File handle returned by the FileOpen() function. 
size   - Number format size. Can be DOUBLE_VALUE(8 bytes) or FLOAT_VALUE(4 bytes). 

Sample:

  int handle;
  double value;
  handle=FileOpen("mydata.dat",FILE_BIN);
  if(handle>0)
    {
     value=FileReadDouble(handle,DOUBLE_VALUE);
     FileClose(handle);
    }

FileReadInteger()

int FileReadInteger(int handle, int size=LONG_VALUE)

The function reads the integer from the current binary file position. The integer format size can be 1, 2 or 4 bytes. If the format size is not specified, the system tries to read the 4-bytes value.

To get the detailed error information, one has to call the GetLastError() function.

Parameters:

handle - File handle returned by the FileOpen() function. 
size   - Format size. Can be CHAR_VALUE(1 byte), SHORT_VALUE(2 bytes) or LONG_VALUE(4 bytes). 

Sample:

  int handle;
  int value;
  handle=FileOpen("mydata.dat", FILE_BIN|FILE_READ);
  if(handle>0)
    {
     value=FileReadInteger(h1,2);
     FileClose(handle);
    }

FileReadNumber()

double FileReadNumber(int handle)

Read the number from the current file position before the delimiter. Only for CSV files.

To get the detailed error information, one has to call the GetLastError() function.

Parameters:

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

Sample:

  int handle;
  int value;
  handle=FileOpen("filename.csv", FILE_CSV, ';');
  if(handle>0)
    {
     value=FileReadNumber(handle);
     FileClose(handle);
    }

FileReadString()

string FileReadString(int handle, int length=0)

The function reads the string from the current file position. Applies to both CSV and binary files. For text files, the string will be read before the delimiter. For binary file, the given count of characters will be read to the string.

To get the detailed error information, one has to call the GetLastError() function.

Parameters:

handle - File handle returned by the FileOpen() function. 
length - Amount of characters for reading. 

Sample:

  int handle;
  string str;
  handle=FileOpen("filename.csv", FILE_CSV|FILE_READ);
  if(handle>0)
    {
     str=FileReadString(handle);
     FileClose(handle);
    }

FileSeek()

bool FileSeek(int handle, int offset, int origin)

The function moves the file pointer to a new position that is an offset, in bytes, from the beginning, the end or the current file position. The next reading or writing are made at a new position.

If file pointer has been moved successfully, the function returns TRUE, otherwise, it returns FALSE. To get the detailed error information, one has to call the GetLastError() function.

Parameters:

handle - File handle returned by the FileOpen() functions. 
offset - Offset, in bytes, from origin. 
origin - Initial position. Value can be one of the following constants:
         SEEK_CUR - from current position,
         SEEK_SET - from begin,
         SEEK_END - from end of file. 

Sample:

  int handle=FileOpen("filename.csv", FILE_CSV|FILE_READ|FILE_WRITE, ';');
  if(handle>0)
    {
     FileSeek(handle, 0, SEEK_END);
     //---- add data to the end of file
     FileWrite(handle, data1, data2);
     FileClose(handle);
     handle=0;
    }
This article url: http://www.myeatrade.com/444/

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