MQL4 Reference File Functions FileSeek


The function moves the position of the file pointer by a specified number of bytes relative to the specified position.

bool  FileSeek(
   int                  file_handle,     // File handle
   long                 offset,          // In bytes
   ENUM_FILE_POSITION   origin           // Position for reference



[in]  File descriptor returned by FileOpen().


[in] The shift in bytes (may take a negative value).


[in] The starting point for the displacement. Can be one of values of ENUM_FILE_POSITION.

Return Value

If successful the function returns true, otherwise false. To obtain information about the error call the GetLastError() function.


If the execution of the FileSeek() function results in a negative shift (going beyond the "level boundary" of the file), the file pointer will be set to the file beginning.

If a position is set beyond the "right boundary" of the file (larger than the file size), the next writing to the file will be performed not from the end of the file, but from the position set. In this case indefinite values will be written for the previous file end and the position set.


//|                                                Demo_FileSeek.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                     |
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      ""
#property version   "1.00"
//--- display the window of input parameters when launching the script
#property script_show_inputs
//--- input parameters
input string InpFileName="file.txt";    // file name
input string InpDirectoryName="Data";   // directory name
input int    InpEncodingType=FILE_ANSI// ANSI=32 or UNICODE=64
//| Script program start function                                    |
void OnStart()
//--- specify the value of the variable for generating random numbers
//--- variables for positions of the strings' start points
   ulong pos[];
   int   size;
//--- reset the error value
//--- open the file
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_TXT|InpEncodingType);
      PrintFormat("%s file is available for reading",InpFileName);
      //--- receive start position for each string in the file
      //--- define the number of strings in the file
         //--- stop if the file does not have strings
         PrintFormat("%s file is empty!",InpFileName);
      //--- make a random selection of a string number
      int ind=MathRand()%size;
      //--- shift position to the starting point of the string
      //--- read and print the string with ind number
         PrintFormat("String text with %d number: \"%s\"",ind,FileReadString(file_handle));
      //--- close the file
      PrintFormat("%s file is closed",InpFileName);
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
//| The function defines starting points for each of the strings in the file and  |
//| places them in arr array                                                      |
void GetStringPositions(const int handle,ulong &arr[])
//--- default array size
   int def_size=127;
//--- allocate memory for the array
//--- string counter
   int i=0;
//--- if this is not the file's end, then there is at least one string
      return// the file is empty, exit
//--- define the shift in bytes depending on encoding
   int shift;
//--- go through the strings in the loop
      //--- read the string
      //--- check for the file end
         //--- store the next string's position
         //--- increase the size of the array if it is overflown
         break// end of the file, exit
//--- define the actual size of the array