myeatrade

MQL4 Reference

MQL4 File functions (3)

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.

 

FileSize()

int FileSize(int handle)

The function returns file size in bytes. 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 size;
  handle=FileOpen("my_table.dat", FILE_BIN|FILE_READ);
  if(handle>0)
    {
     size=FileSize(handle);
     Print("my_table.dat size is ", size, " bytes");
     FileClose(handle);
    }

FileTell()

int FileTell(int handle)

Returns the current position of the file pointer. To get the detailed error information, one has to call GetLastError() function.

Parameters:

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

Sample:

  int handle;
  int pos;
  handle=FileOpen("my_table.dat", FILE_BIN|FILE_READ);
  // reading some data
  pos=FileTell(handle);
  Print("current position is ", pos);

FileWrite()

int FileWrite(int handle, ...)

The function is intended for writing of data into a CSV file, delimiter being inserted automatically. After writing into the file, the line end character "\r\n" will be added. Numbers will be converted into a text at output (see the Print() function).

Returns the count of written characters or a negative number if an error occurs.

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

Parameters:

handle - File handle returned by the FileOpen() function.  
...    - User data to write, separated by commas. It can be up to 63 parameters.
         Data of int and double types are automatically converted into a string, 
         but those of color, datetime and bool typesare not automatically converted 
         and will be written to file as they are (as integers).
         Arrays cannot be passed as a parameter, they can be output elementwise. 

Sample:

  int handle;
  datetime orderOpen=OrderOpenTime();
  handle=FileOpen("filename", FILE_CSV|FILE_WRITE, ';');
  if(handle>0)
    {
     FileWrite(handle, Close[0], Open[0], High[0], Low[0], TimeToStr(orderOpen));
     FileClose(handle);
    }

FileWriteArray()

int FileWriteArray(int handle, object array[], int start, int count)

The function writes the array to a binary file. Arrays of int, bool, datetime and color types will be written elementwise, as 4-bytes integers. Arrays of double type will be written elementwise, as 8-bytes floating point numbers. Arrays of string type will be written by strings, the line end character "\r\n" to be added at each string automatically.

Returns the number of written elements or a negative value if an error occurs.

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

Parameters:

handle  - File handle returned by the FileOpen() function.  
array[] - Array to write. 
start   - Starting index in the array (the first written element number). 
count   - Count of elements to be written. 

Sample:

  int handle;
  double BarOpenValues[10];
  // copy first ten bars to the array
  for(int i=0;i<10; i++)
    BarOpenValues[i]=Open[i];
  // writing array to the file
  handle=FileOpen("mydata.dat", FILE_BIN|FILE_WRITE);
  if(handle>0)
    {
     FileWriteArray(handle, BarOpenValues, 3, 7); // writing last 7 elements
     FileClose(handle);
    }

FileWriteDouble()

int FileWriteDouble(int handle, double value, int size=DOUBLE_VALUE)

The function writes a double value with floating point to a binary file. If the format is specified as FLOAT_VALUE, the value will be written as a 4-bytes floating point number (of the float type), otherwise, it will be written in the 8-bytes floating point format (of the double type).

Returns the actually written bytes count or a negative value if an error occurs.

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

Parameters:

handle - File handle returned by the FileOpen() function.  
value  - Double precision value. 
size   - Optional format flag. It can be any of the following values:
         DOUBLE_VALUE (8 bytes, default)
         FLOAT_VALUE (4 bytes). 

Sample:

  int handle;
  double var1=0.345;
  handle=FileOpen("mydata.dat", FILE_BIN|FILE_WRITE);
  if(handle<1)
    {
     Print("can't open file error-",GetLastError());
     return(0);
    }
  FileWriteDouble(h1, var1, DOUBLE_VALUE);
  //...
  FileClose(handle);

FileWriteInteger()

int FileWriteInteger(int handle, int value, int size=LONG_VALUE)

The function writes the integer value to a binary file. If the size is SHORT_VALUE, the value will be written as a 2-byte integer (the short type), if the size is CHAR_VALUE, the value will be written as a 1-byte integer (the char type), and if the size is LONG_VALUE, the value will be written as a 4-byte integer (the long int type).

Returns the actually written bytes count or a negative value if an error occurs.

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

Parameters:

handle - File handle returned by the FileOpen() function.  
value  - Value to be written. 
size   - Optional format flag. It can be any of the following values:
         CHAR_VALUE (1 byte),
         SHORT_VALUE (2 bytes),
         LONG_VALUE (4 bytes, default). 

Sample:

  int handle;
  int value=10;
  handle=FileOpen("filename.dat", FILE_BIN|FILE_WRITE);
  if(handle<1)
    {
     Print("can't open file error-",GetLastError());
     return(0);
    }
  FileWriteInteger(handle, value, SHORT_VALUE);
  //...
  FileClose(handle);

FileWriteString()

int FileWriteString(int handle, string value, int length)

The function writes the string to a binary file from the current file position.

Returns the actually written bytes count or a negative value if an error occurs.

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

Parameters:

handle - File handle returned by the FileOpen() function.  
value  - String to be written. 
length - The length of the string to be written. 
         If the string length exceeds the given value, it will be truncated. 
         If it is shorter, it will be extended by binary 0s up to the given length. 

Sample:

  int handle;
  string str="some string";
  handle=FileOpen("filename.bin", FILE_BIN|FILE_WRITE);
    if(handle<1)
    {
     Print("can't open file error-",GetLastError());
     return(0);
    }
  FileWriteString(handle, str, 8);
  FileClose(handle);
This article url: http://www.myeatrade.com/445/

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