You are on page 1of 27

MT4-5 Working with Files

Working with files . Part I


December 31, 2009 By Bogdan Baltatu, MQLmagazine editor [Versiunea romaneasca] [MQLmagazine.com in romana] [English edition] This article will be about working with files and folders in MQL5. Functions working with files in MQL5 can be divided in two groups:

Functions that deal with file manipulation and status of files Functions that deal with reading and writing in files

I will take every function group and describe its functionality with code examples. Functions that deal with file manipulation and status of files These functions are the ones that open, close, delete, copy, force writing, check files existance and pointer position within files. These are the functions considered by us in this category :
FileClose, FileCopy, FileDelete, FileFindCLose, FileFindFirst, FileFindNext, FileFlush, FileIsEnding, FileExist, FileIsLineEnding, FileMove, FileOpen, FileSeek, FileTell

Flags Part of the function presented above use flags. These flags are global integer constants defined by MQL5 and have the purpose to specify the file opening modes (text,csv,ansi,binary) and the operations done on the file (reading/writing). The table below contains the binary value but also the integer value of the identifiers, because these are important for understanding how flags work: Decimal value 1 0000000000001 FILE_READ File open for reading
1|Page

Binary value

Flag identifier

Description

MT4-5 Working with Files

2 4 8 16 32 64 128 256 512 4096

0000000000010 FILE_WRITE 0000000000100 FILE_BIN 0000000001000 FILE_CSV 0000000010000 FILE_TXT 0000000100000 FILE_ANSI 0000001000000 FILE_UNICODE

File open for writing Binary access for reading/writing (no strings) CSV file (Text file with elements separated) Text file ANSI file (one byte symbols) UNICODE file (two byte symbols)

0000010000000 FILE_SHARE_READ Acces impartit pentru citire de mai multe programe 0000100000000 FILE_SHARE_WRITE Shared access for more programs 0001000000000 FILE_REWRITE 1000000000000 FILE_COMMON Possibility to rewrite the file with FileCopy and FileMove. File is in the common folder for all MT5 client terminals

Comparing with the previous MetaQuotes language, MQL4, can be observed the birth of new flags like the ones that give the possibility to open files in new modes like : ANSI, text, UNICODE but also two new modes that allow writing and reading from files even if they are opened by other programs. A novelty brought by MetaQuotes is opening files that are in MQL5 folder and not only in terminal_directory\experts\files as it was possible in MT4. Functions working with files may accept flag combinations, but these flags must be used carefully, because for instance you cant use FILE_TXT|FILE_BIN simultaneously because its impossible to open the same file in two modes. Ill detail the correct flag usage at the FileOpen() function. FileOpen The FileOpen() function sits at the base of file operations because there couldnt be any continuation with other operations if files cant be created or opened.

2|Page

MT4-5 Working with Files

1 int FileOpen(string file_name, 2 3 4 ); int open_flags, short delimiter //for CSV files

The function returns an integer (file handle) and this integer has to be memorated in a variable because it will be used with all operations with the file, representing the reference to the opened or created file. The FileOpen() function has a double role because it is used for both opening and creating files.
1 int m_handle=-1; 2 string m_filename="test.txt"; 3 m_handle=FileOpen(m_filename,FILE_READ|FILE_TXT); 4 if (m_handle<0) 5 6 7 } { Print("I can't open the file.");

8 else 9 Print("File successfully open.");

The above code opens the test.txt file in the text mode. If the file doesnt exist, m_handle will remain -1, what means the file hasnt been open yet. (Surely cant be opened since it doesnt exist). If we wish to create a file we have to use FILE_WRITE because the flag is creating the file in the same time. The created file is in the /MQL5/Files/ folder. In the beta version, the one existing now at the time the article is written, the files are not in the MQL5 folder thats where you installed the platform. To see the exact place where the files are you can use in a script the following line:
1 Print(TerminalInfoString(TERMINAL_DATA_PATH));

Be careful using flags, because wrong use can generate errors. FileClose

3|Page

MT4-5 Working with Files

1 2 3

void int );

FileClose( file_handle

The file_handle parameter is the one returned by FileOpen(), in the FileOpen() example the name of our parameter is m_handle. Its good to reset this parameter to -1 after we close the file if we want to check if the file is still opened. The FileClose() function doesnt return any result meaning that, at least theoretically the function is successful. FileDelete The function deletes a file , be it in the local terminal folder or in the common folder used by all MetaTrader5 platforms.
1 bool 2 3 4 FileDelete( file_name common_flag=0 // Numele fisierului care va fi sters // Flag de localizare

string int );

The second parameter is optional. If it is FILE_COMMON then the function will delete the file from the folder thats shared by all MetaTrader5 platforms; otherwise, it will delete the file from the local station. The function returns true if the operation was successful, or false in case that it failed. Functia returneaza true daca operatiunea a fost executata cu succes sau false in caz de operatiunea a esuat. MetaTrader experts can be run realtime or they can be tested on historical data. When experts are being tested on historical data the functions working with files dont use MQL5/files anymore, but MQL5/tester instead. FileCopy The function copies a folder from the local or common folder to another name keeping the file intact.
1 bool 2 FileCopy( string src_filename, //Name of the file to be copied

4|Page

MT4-5 Working with Files

3 4 5 6 );

int string int

common_flag, dst_filename, mode_flags

//Localization flag //Destination file name //Access flags

The second parameter, common_flag, cant be missing so when the file we want to copy is not in the MT5 shared folder it has to be replaced with 0. The mode_flags parameter has two options : FILE_REWRITE and/or FILE_COMMON. If the destination file exists and we try to copy a file without specifying FILE_REWRITE as mode_flags parameter then the destination file will not be overwritten by the source. FileMove The functions prototype is the same as FileCopy()s prototype. The difference is that the FileMove() copies the source file in another location but in the same time deletes the source. Practically the function can be used to rename an existing file. FileFlush The function has the role to write on the disc the data from the buffer.
1 2 3 void FileFlush( int ); file_handle

The function has only one parameter, that being the file handle resulted by open/creation with FileOpen(). To understand what FileFlush() is used for, we have to understand how file reading/writing works. When a file is opened the information is copied in memory in a buffer, because memorys speed is higher than disk speed. A call to FileFlush() is mandatory whenever operation is changed , from reading to writing or the other way around. FileIsExist The function FileIsExist checks whether the file exists or not.
1 2 bool FileIsExist( string file_name,

5|Page

MT4-5 Working with Files

3 4

int );

common_flag=0

The first parameter refers the name of the file, and the second parameter refers the place where the file has to be looked for. The function can be used to limit the errors generated by FileOpen() in the case the file doesnt exist. FileTell The function returns the current position of a file pointer in an open file.
1 2 3 int FileTell( int ); file_handle

The position is in number of bytes from the beginning of the file. FileIsEnding The function is used to find out whether the file end has been reached or not.
1 bool 2 3 FileIsEnding( int ); file_handle // Handler de fisier

The function is necesary because its important to know when the end of the file has been touch, in the most cases when file is read entirely or when we search for something in it. FileIsLineEnding The function has the same prototype as the FileIsEnding() function, just this returns true when the end of the line is being touched instead of the end of the file. The function can be used only for text or CSV files and the end of the line is when CR (Carriage Return) or LF(Line Feed) is encountered. However, the function is quite buggy at the time the article is being written. FileSeek The function moves the file pointer a specified number of bytes from the specified position.

6|Page

MT4-5 Working with Files

1 void 2 3 4 5

FileSeek( long int ENUM_FILE_POSITION ); file_handle offset, origin

The origin can be SEEK_SET (begin of the file), SEEK_CUR (current position), SEEK_END (sfarsitul fisierului). The offset can be either positive or negative because the pointer can move in both directions. The last 3 functions that are presented in this article are FileFindFirst(), FileFindNext() and FileFindClose(). FileFindFirst The function has the purpose to begin enumerating files in the local or common folder with a specified wildcard.
1 2 3 4 5 long FileFindFirst( string string& int ); file_filter, returned_filename, common_flag

The first parameter is the wildcard. Wildcard examples : - *.* all files; - *.txt text files (to be read files with a .txt extension) - test.* files named test (any extension) The FileFindFirst() function returns a search handler . The second parameter is the first file that matches the search, and the third parameter is the location flag. FileFindNext The function resembles FileFindFirst() , just that the prototype is different:
1 2 bool FileFindNext( long search_handle,

7|Page

MT4-5 Working with Files

3 4 );

string&

returned_filename

The function resumes a previously started search with FileFindFirst() (it knows this from the first parameter, search_handle). The function returns in the second parameter the following file matching the wildcard. It returns true if it fills this parameter with a valid file; if it fails to do that, and this happens at the end of the search, it returns false. FileFindClose The function destroys a search handler. Prototipul functiei este:
1 2 3 ); void FileFindClose( long search_handle

It is necessary use the function in order to release the resources allocated by the call of FileFindFirst(). The function prototypes and even names may be changed by MetaQuotes because at moment this article is written the platform and the MetaEditor are still beta. If you find any mistakes in the article please report.

8|Page

MT4-5 Working with Files

Working with files. Part II


January 28, 2010 By Bogdan Baltatu, MQLmagazine editor [Versiunea romaneasca] [MQLmagazine.com in romana] [English edition] This article continues the precedent article Working with files . Part I and its intended to be its practical side. The article shows in one code the use of the functions shown in Part I. Before this, a few notes. MetaQuotes changed the prototype of the FileOpen() function.
1 int 2 3 4 5 6 FileOpen( string int short uint ); file_name, open_flags, delimiter='\t' codepage=CP_ACP // File name // Combination of flags // Delimiter // Code page

What was added is the codepage, that serves at the conversion of string variables. Default is CP_ACP (Ansi Code Page). To be known when using FileOpen(): if FILE_ANSI is not used, FILE_UNICODE is presumed; even the end of the line is awaited as unicode (CRLF spanning 4 bytes instead of 2). Regarding the FileIsLineEnding() function, whose application seemed unclear in the previous article, there is a small glitch to be known: indeed, function works only on FILE_TXT files, and it answers true if the end of the line is reached by using FileReadString(). Not by seeking. However, remember that most text files are ANSI. Dont forget to specify FILE_ANSI. Otherwise all readings will be abnormal, including the fact that FileIsLineEnding() will not report correctly!
1 //+------------------------------------------------------------------+ 2 //| WorkWithFiles.mq5 |

9|Page

MT4-5 Working with Files

3 //| 4 //|

Copyright Baltatu Bogdan | http:\\mqlmagazine.com |

5 //+------------------------------------------------------------------+ 6 #property copyright "Baltatu Bogdan" 7 #property link 8 #property version "http:\\mqlmagazine.com" "1.00"

9 //+------------------------------------------------------------------+ 10 //| Script program start function |

11 //+------------------------------------------------------------------+ 12 13 void OnStart() 14 15 16 17 18 { int file_handle,size=-1,i; bool res; string svalue,filename; long search;

19 //FileOpen 20 21 22 file_handle=FileOpen("test.txt",FILE_WRITE|FILE_TXT); if(file_handle==-1) Print("FileOpen error.ErrorNo:",GetLastError()); else Print("FileOpen success!");

23 //FileIsExist 24 25 26 res=FileIsExist("test.txt"); if(res)Print("File exist!!"); else Print("File not found!!");

27 //FileWrite 28 29 30 svalue="abcdef\r\nghijkl"; size=StringLen(svalue); FileWriteString(file_handle,svalue,size);

31 //FileFlush 32 FileFlush(file_handle);

33 //FileClose 34 35 FileClose(file_handle); file_handle=FileOpen("test.txt",FILE_TXT|FILE_READ);

36 //FileTell 37 38 FileTell(file_handle); Print("Cursor position:",FileTell(file_handle));

10 | P a g e

MT4-5 Working with Files

39 //FileSeek 40 FileSeek(file_handle,0,SEEK_SET);

41 //FileClose 42 FileClose(file_handle);

43 //FileCopy 44 45 46 res=FileCopy("test.txt",0,"test1.txt",FILE_REWRITE); if(res) Print("FileCopy Succes"); else Print("FileCopy Faild.ErrorNo:",GetLastError());

47 //FileMove 48 49 50 res=FileMove("test.txt",0,"move_test.txt",FILE_REWRITE); if(res) Print("FileMove Succes!"); else Print("FileMove Faild!ErrorNo:",GetLastError());

51 //FileDelete 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 } FileDelete("test.txt"); } else Print("Files not found!!!"); } FileFindClose(filename); search=FileFindFirst("*.*",filename); if(search!=INVALID_HANDLE) { Print("FileFindFirst returned",filename); while(FileFindNext(search,filename)) { i++; Print(i,":",filename);

68 //+------------------------------------------------------------------+

11 | P a g e

MT4-5 Working with Files

Working with files. Part III


February 28, 2010 By Bogdan Baltatu, MQLmagazine editor [Versiunea romaneasca] [MQLmagazine.com in romana] [English edition] This is the third article of the Working with files series and I care to announce you that this is one of the most important articles of the series because Ill present the writing functions as well as examples that will show some aspects of the functions. The functions that refer strictly file writing are 8. These functions are:
FileWrite() , FileWriteArray() , FileWriteDouble() , FileWriteFloat() , FileWriteInteger() , FileWriteLong() , FileWriteString() , FileWriteStruct()

1 2

In the continuation Ill present each function with its notes. FileWrite() The function is used for writing data in CSV files. The prototype of the function is:
1 uint 2 3 4 int ... ); FileWrite( file_handle // file handle // List of parameters to be written

The file_handle parameter represents the result of the FileOpen() function, that has to be called with the flags FILE_CSV|FILE_WRITE. If the file is not open with the FILE_WRITE flag, then FileWrite() will fail. The parameter list to be written in the CSV file cannot be larger than 63, parameters will be separated by the delimiter specified when we opened the file with the FileOpen() function. Should we have not specify it at that time, the tab delimiter would have been default (ASCII 9). The parameter type is not specified, they could be integer, datetime , float , . It is important to know that the FileWrite() function converts the parameters in strings.
12 | P a g e

MT4-5 Working with Files

The double parameters have 16 digits after the decimal dot, the float parameters have 5, and the datetime parameters are converted to the YYYY.MM.DD HH:MI:SS. Each FileWrite() command puts at the end of the generated string \r\n. This shows us that after writing the string the cursor moves to the next row.
1 int file_handle=FileOpen("text.csv",FILE_ANSI|FILE_CSV|FILE_WRITE); 2 uint chars_written=FileWrite(file_handle,1,3,4,5,6,7);

In Excel 2007, even if delimiter is tab and the parameters should be displayed on a separate column they dont show up. If we rename the file to .txt and we open it with Excel 2007 then parameters will be each on a column because it admits the tab character. The issue is not with Excel and it deserves some study. It is recommended to open the CSV with an editor as notepad or notepad++ if it has more than 65535 lines. FileWriteArray() The function writes elements of different types of array, except for the string ones, in files opened as FILE_BIN. The prototype of the function is:
1 2 3 4 5 6 int int void int int ); FileWriteArray( file_handle array[], start_item=0, items_count=WHOLE_ARRAY

The parameters of the function are file_handle , the result of the FileOpen(); array , the array that is source for the elements to be written in the file; start_item , position in the array where to start writing from; items_count , the number of elements to be copied starting from start_item. The result of the FileWriteArray() function is the number of elements written in the file, so the result of the function has to be equal to items_count for the function call to be considered successfully.

13 | P a g e

MT4-5 Working with Files

If the table has 9 elements and we send an items_count larger than 9, the FileWriteArray() will not return any error, because it will write all the 9 elements. Below you have a code example which writes a matrix in a binary file (opened with FILE_BIN):
1 double array1[3][3]={{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}, {7.0, 8.0, 9.0}}; 2 int file_handle=FileOpen("text.bin",FILE_BIN|FILE_WRITE); 3 int elements_written=FileWriteArray(file_handle,array1,0,WHOLE_ARRAY);

String arrays have to be written in TXT files. Each element, when written in the file, is followed by the \r\n characters, so each table element is written on a new line. Below we exemplified in a code sample the functionality of FileWriteArray() if the matrix is string.
1 string array1[3][3]={{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}}; 2 int file_handle=FileOpen("text.txt",FILE_TXT|FILE_WRITE|FILE_ANSI); 3 int elements_written=FileWriteArray(file_handle,array1,6,3);

FileWriteDouble() The function writes in a file from the current cursor position a value of the double type. The functions prototype is:
1 2 3 4 uint int double ); FileWriteDouble( file_handle dvalue

Parameters are two: file_handle is the file handle returned by FileOpen() and dvalue is the double value that we wish to have it written in the file. The function is considered successful if the result is 8, meaningly the length in bytes of a double type variable, and the pointer moves by 8 bytes. Below is a code example.
1 fh=FileOpen("test.bin",FILE_WRITE|FILE_BIN);

14 | P a g e

MT4-5 Working with Files

s=FileWriteDouble(fh,a);

Warning: To use the FileWriteDouble() function, the file has to be opened binary, otherwise the function fails. FileWriteFloat() The FileWriteFloat() function resembles FileWriteDouble() , only that the variable to be written is of float type. The functions prototype is:
1 2 3 4 uint int float ); FileWriteFloat( file_handle fvalue

The function parameters are file_handle , the reference of the file given by FileOpen() and the float value that we wish to have it written in our file. Code example.
1 fh=FileOpen("test.bin",FILE_WRITE|FILE_BIN); 2 s=FileWriteFloat(fh,a);

Of course, the result has to be 4, the length in bytes of a float type variable, for a correct writing. FileWriteInteger() The function writes an integer typed variable in a binary file beginning from the current pointer. The function's prototype is:
1 2 3 4 5 uint int int int ); FileWriteInteger( file_handle ivalue, size=INT_VALUE

15 | P a g e

MT4-5 Working with Files

The parameters of the function are : the file handle, the value that we wish to have written, and its length in bytes. The last parameter may be 1, 2 or 4 (1=char , 2=short, 4=integer). The function returns 1,2 or 4 accordingly to the size parameter and the returned value has to be equal with this, if the execution was correct. Code example:
1 int a=2; 2 3 fh=FileOpen("test.bin",FILE_WRITE|FILE_BIN); s=FileWriteInteger(fh,a,4);

The file has to be opened as binary, otherwise the function fails. If the function is correctly executed then pointer moves with as much bytes as the function returns (1, 2 or 4). We have to specify that by 'int' in the function's prototype the compiler actually understands any integer type, and the function does not automatically presume signed integers, being able to work with unsigned integers. FileWriteLong() The function writes a long typed value ion a binary file beginning with the current cursor position in the file. The function's prototype is:
1 2 3 4 uint int long ); FileWriteLong( file_handle lvalue

The parameters of the function are : the file handle, returned by the FileOpen() call and the value that we want to have written. If the function executes successfully, it returns 8, the length in bytes of a long typed variable. Code example
1 long a=212313123; 2 fh=FileOpen("test.bin",FILE_WRITE|FILE_BIN);

16 | P a g e

MT4-5 Working with Files

3 s=FileWriteLong(fh,a);

FileWriteString() The function writes a string in text or binary file beginning with the current position of the cursor. The function's prototype is:
1 2 3 4 5 uint int string int ); FileWriteString( file_handle svalue, size=-1

The parameters of the function are : the file handle, the string that we want to have written, and the number of characters contained by the string. The third parameter is mandatory if the file has been opened with FILE_BIN, but optional when the file has been opened as FILE_TXT. If the execution is successful then the result is the number of bytes written in the file and the cursor's position moves by this number of bytes. It should be noted that when we use the function to write in a FILE_UNICODE file (specified or not, because FILE_UNICODE is default), the number of bytes written is twice the number of characters. When we write with FILE_ANSI flag, the number of bytes is the same as the number of characters. MetaQuotes' decision is a bit awkward , to make FILE_UNICODE default instead of FILE_ANSI. It should be normal that FILE_ANSI is default, De precizat ar fi ca atunci cand folosim functia pentru a scrie intr-un fisier deschis cu FILE_UNICODE (specificat sau nu, caci FILE_UNICODE e default) numarul de bytes scrisi este de 2 ori mai mare decat numarul de caractere. Cand scriem cu flagul FILE_ANSI numarul de bytes scrisi coincide cu numarul de caractere. E cel putin ciudata decizia MetaQuotes sa faca FILE_UNICODE default in loc de FILE_ANSI. Normal ar fi ca FILE_ANSI sa fie default, to avoid the useless doubling of the text files, being known that the majority of users will open them with Notepad or Excel. Code example.

17 | P a g e

MT4-5 Working with Files

string a="A1b2C3";

2 fh=FileOpen("test.txt",FILE_WRITE|FILE_ANSI); 3 s=FileWriteString(fh,a);

FileWriteStruct() The function writes in a binary typed file the contents of a structure parsed as parameter beginning with the current cursor position. The prototype of the function is:
1 2 3 4 5 uint int any_simple_struct int ); FileWriteStruct( file_handle str_object&, size

The function parameters are : the file handle returned by FileOpen(), the structure (by reference) and the length in bytes that has to be written. If the function is executed successfully it returns the number of written bytes. Code example:
1 MqlRates a[1];

2 CopyRates(Symbol(),PERIOD_D1,0,1,a); 3 int so=sizeof(a); 4 fh=FileOpen("te.bin",FILE_WRITE|FILE_BIN); 5 s=FileWriteStruct(fh,a,so);

When we define the structure 'a', we have to define it as array, to be able to use it with the CopyRates() function, and it has to have at least one element. It should be known that the files opened with FILE_BIN where we wrote something will contain weird characters if opened. They are not wrongly written. Even if the files are binary we saved them as '.txt' to be easily to be opened with the text editor for you to see easier that something was written. It seems that MetaQuotes has chosen a complicated path for files. I'll make a short review on how Borland Pascal knew files, so you can see how simple was at that
18 | P a g e

MT4-5 Working with Files

time (the '90s). First, Borland Pascal didn't have the concept of file handle, a number to identify the file. It had only file typed variables. These were complex variables, known only by compiler - they couldn't have been read, written or interogated. The functions that dealt with reading/writing were about 6 : ReadLn, WriteLn, Read, Write, BlockRead, BlockWrite. ReadLn and WriteLn were working only with the screen, keyboard and text files, while Read and Write were also working on the "binary" level. Due to the special nature of the file variables, Read and Write knew from the beginning if they have an I/O operation with keyboard/screen or with a file. There was no need of FileRead or FileWrite, because if the first parameter was a file typed variable, it couldn't have been mistaken with an integer or anything else - the functions knew from beginning what they work with. If it was file of text , and you were writing Write(f,128) , the function would have written "128", text, on 3 characters, and if the file would have been file of byte, the function would have written a byte with the 128 value. Also, the strict definition of variables made the difference between types and functions knew from beginning how many bytes they had to read/write for any data type. If file had a complex type , like file of record (record was the equivalent for struct from MQL5), then functions were reading or writing one by one many structures in a row, incrementing the cursor with the number of structures, not with the number of bytes. BlockRead and BlockWrite were working with any kind of variables, being interested only in the length of the read/written blocks (they were working with untyped files). Be careful when using file functions. Otherwise, you can lose time recording data that are saved in a wrong manner!

19 | P a g e

MT4-5 Working with Files

Working with files. Part IV


March 31, 2010 By Bogdan Baltatu, MQLmagazine editor [Versiunea romaneasca] [MQLmagazine.com in romana] [English edition] This article is the last from the Working with files series and it will treat the file reading functions exclusively. Functions that deal with file reading are :
FileReadArray(), FileReadBool(), FileReadDatetime(), FileReadDouble(), 1 FileReadFloat(), 2 FileReadInteger(), FileReadLong(), FileReadNumber(), FileReadString(), FileReadStruct()

FileReadArray() The function reads from a binary file arrays of any type except for string. The functions prototype is:
1 uint 2 int 3 void 4 int 5 int 6 ); FileReadArray( file_handle array[], start_item=0, items_count=WHOLE_ARRAY // File handle // Array to record // start array to write // items count

The function parameters are : the file handle, the array being recorded in, the position where reading begins and the number of elements to be read. The result is the number of read elements. Code example:
1 double b[]; 2 int fh=FileOpen("test.bin",FILE_BIN|FILE_READ|FILE_WRITE); 3 FileWriteDouble(fh,5.123); 4 FileWriteDouble(fh,1.2123);

20 | P a g e

MT4-5 Working with Files

5 FileSeek(fh,0,SEEK_SET); 6 FileReadArray(fh,b,0,WHOLE_ARRAY); 7 FileClose(fh);

I preferred to write in the binary file using the FileWriteDouble() function (lines 3 and 4) when I could have used the FileWriteArray() function, because I wanted you, the reader, to play with the code and get the conclusion which I present here. On the line 5 I called the function FileSeek() because If I would read immediately, the cursor would be at the end of the file and function wouldnt have read anything , so before using the FileReadArray() you have to be sure the position of the cursor is in the right place. The type of the array must be the same with the type of the elements we wish to read. If for instance we wrote in the file with FileWriteDouble(), the array where data must get by FileReadArray() must be double. If its not, the data will be altered according to the type of the b array ; in our case the function reads sets of 8 bytes each, because a double spans over 8 bytes. Write an element with FileWriteDouble() and one with FileWriteInteger() and make the read in a double typed array to see what happens. After you do this youll draw the conclusion that is better than in a file to keep only one type of function for writing and the array or the structure where you read to be the same type as written elements. Be careful about the cursors position! I remind you that the first element in an array is on the 0 position, so in our case the first will be b[0] and the secondary will be b[1]. FileReadBool() The functions prototype is
1 2 3 bool int ); FileReadBool( file_handle // File handle

21 | P a g e

MT4-5 Working with Files

The function reads in a CSV typed file a string and converts to boolean. Functia citeste dintr-un fisier CSV un string si il converteste in boolean; false for 0, true for anything else. FileReadDatetime() The function reads from a CSV file a string in one of the formats: YYYY.MM.DD HH:MI:SS, YYYY.MM.DD or HH:MI:SS and converts them in a datetime value. The functions prototype is:
datetime 1 int FileReadDatetime(

file_handle

2 ); 3 4 The function has just one parameter and that is the file handle 5 retrieved by FileOpen(). 6 7 Code example. 8 9 <pre lang="mql5">int 10 fh=FileOpen("test.csv",FILE_CSV|FILE_READ|FILE_WRITE); 11 FileWrite(fh,"asd 12:54 06:00"); 12 FileSeek(fh,0,SEEK_SET); 13 datetime dt=FileReadDatetime(fh); FileClose(fh);

Note that on line 2 I wrote in the same string two substrings that can be interpreted and converted as date (12:54 and 06:00). The function reads always the first string that can be converted to date from the current cursor position and converts it in a variable of datetime type. If our date is HH:MM then dt would take the value : YYYY.MM.DD 12:54 where YYYY.MM.DD is the current date. FileReadDouble() The function reads a double from the current cursor position in a binary file. The functions prototype is:
22 | P a g e

MT4-5 Working with Files

1 2 3

double int );

FileReadDouble( file_handle

The function has a single parameter and that is the file handle returned by FileOpen() and the functions result is a double. Code example:
1 int fh=FileOpen("test.bin",FILE_BIN|FILE_READ|FILE_WRITE);

2 FileWriteDouble(fh,1.1234); 3 FileWriteDouble(fh,0.0123); 4 FileSeek(fh,8,SEEK_SET); 5 double dvalue=FileReadDouble(fh); 6 FileClose(fh);

I wrote two double typed values in the file. If we play with the second parameter of the FileSeek() function, we will observe that the numbers differs from the typed one because the function reads 8 bytes from the cursor position and the number returned by FileReadDouble() will be different from the whats written. FileReadFloat() The function reads a float from a binary file. The functions prototype is:
1 2 3 float int ); FileReadFloat( file_handle

The function has a single parameter and that is the file handle retrieved by FileOpen(). Code example:
1 int fh=FileOpen("test.bin",FILE_BIN|FILE_READ|FILE_WRITE); 2 FileWriteFloat(fh,0.3f); 3 FileSeek(fh,0,SEEK_SET); 4 float fvalue=FileReadFloat(fh);

23 | P a g e

MT4-5 Working with Files

5 FileClose(fh);

Same note as above: beware the cursors position!! FileReadInteger() The function reads and integer, short or char from the current pointer position according to the specified bytes length. The functions prototype is:
1 int 2 3 int int FileReadInteger( file_handle size=INT_VALUE // File handle // Size of an integer in bytes

4 );

The functions parameter are the file handle retrieved by FileOpen() and the number of bytes to be read from the current cursor position. (1 for char, 2 for short, 4 for int). Exemplu de cod:
1 int fh=FileOpen("test.bin",FILE_BIN|FILE_READ|FILE_WRITE);

2 FileWriteInteger(fh,15867,4); 3 FileSeek(fh,0,SEEK_SET); 4 int ivalue=FileReadInteger(fh,4); 5 FileClose(fh);

Even if I repeat myself one more time : beware at the cursor position and the number of bytes to be read! Chiar daca ma repet mai adaug o data : atentie la pozitia cursorului si atentie la numarul de bytes pe care il citim. FileReadLong() The function reads a long integer (8 bytes) from the current position of a file opened as binary. The functions prototype is:
1 2 3 long int ); FileReadLong( file_handle

24 | P a g e

MT4-5 Working with Files

The function has only one parameter, the file handle returned by FileOpen(). Code example:
<pre lang="mql5"> int fh=FileOpen("test.bin",FILE_BIN|FILE_READ|FILE_WRITE); FileWriteInteger(fh,15867,8); FileSeek(fh,0,SEEK_SET); int ivalue=FileReadLong(fh); FileClose(fh);

1 2 3 4 5

FileReadNumber() The function reads from a CSV file a string from the current position until it finds the separator then converts the string in a double. The functions prototype is:
1 2 3 double int ); FileReadNumber( file_handle

The function has a single parameter and that is the file handle returned by FileOpen(). Code example:
1 int fh=FileOpen("test.csv",FILE_CSV|FILE_READ|FILE_WRITE); 2 FileWrite(fh,1.2); 3 FileSeek(fh,0,SEEK_SET); 4 double dvalue=FileReadNumber(fh); 5 FileClose(fh);

If on line 2 we would have had A, then FileReadNumber() would have returned 0. FileReadString() The function reads from a string at the current position. The function has the following prototype:
25 | P a g e

MT4-5 Working with Files

1 2 3 4

string int int );

FileReadString( file_handle size=-1

The function has as parameters : file_handle, which is the returned by FileOpen() and size which is the number of characters that we wish to read. Code example:
1 int fh=FileOpen("test.bin",FILE_BIN|FILE_READ|FILE_WRITE);

2 FileWriteString(fh,"abcdef"); 3 FileSeek(fh,0,SEEK_SET); 4 string svalue=FileReadString(fh,3);

If we open the file as text, the secondary parameter is not mandatory, because the FileReadString() function will read until the end of the line marker (\r\n). Also its not mandatory for the case file is opened as CSV, because the function will read until it finds the separator. FileReadStruct() The function reads the from a binary file into a structure, starting with the current pointer position. The functions prototype is:
1 uint 2 int 3 any_simple_struct 4 int 5 ); FileReadStruct( file_handle str_object, size=-1 //File handle // Structura that receives data // Size of structure in bytes

Code example:
1 int fh=FileOpen("test.bin",FILE_BIN|FILE_READ|FILE_WRITE); 2 MqlRates w[1],r[1]; 3 CopyRates(Symbol(),PERIOD_D1,0,1,w); 4 int so=sizeof(w[0]);

26 | P a g e

MT4-5 Working with Files

5 uint s=FileWriteStruct(fh,w[0],so); 6 FileSeek(fh,0,SEEK_SET); 7 FileReadStruct(fh,r[0],so); 8 FileClose(fh);

Id like to note that from MetaEditor build 252 the FileWriteStruct() and FileReadStruct() functions work exclusively with structures, before that were supporting arrays too. In older versions you could have written for instance FileWriteStruct(fh,a,so) but now line is reported with error (a invalid access array).

27 | P a g e

You might also like