NavigationUser loginWho's new
Who's onlineThere are currently 1 user and 63 guests online.
Online users:
|
File functions - Part 1Hi folks, Today we are going to study a very important set of functions that enables us to work with external files from the MQL4 programs; the File functions. Directories limitation:One of annoying feature of MQL4 that the directories limitation; you can't work with files that outside one of these three directories:
MetaTrader thinks it's safer to limit the directories you can access from the normal MQL4 program and give you the ability to write your MQL4 extension (dll) to do what do you want. Note: I've wrote a dll that enable you to work with files outside the limited directories of MQL4, you can know more by going there: http://www.forex-tsd.com/tools-utilities/386-mt4-files-functions-replacement.html There are a lot of functions to study in this section, so, we are going to divide this lesson to two parts each of them study the half of File functions. Let's start this part of the lesson.
FileOpen:Syntax:
Description: The FileOpen function opens the given file for a specific mode (purpose) and returns its handle. You open the file for writing, reading or both and you determine that with the mode parameter. The returned value of the FileOpen function is the file handle if the function successes and -1 if failed. File handle is a unique number assigned for the file which you will use with the most of the File functions to work with the opened file. Parameters: This function takes three parameters: string filename: The file name you want to open. int mode: The purpose you want to open the file for, there are four modes which you can use and combine them: FILE_CSV: use this mode to open the csv (Comma-separated values) file format. FILE_BIN: use this mode to open the binary file format. FILE_READ: use this mode to open the file for reading (getting data from the file). FILE_WRITE: use this mode to open the file for writing (saving data to the file). Note 1: You can not combine the FILE_CSV and FILE_BIN in mode parameter. Note 2: If you open the file with FILE_WRITE and the file wasn't exist a new file with the filename parameter will be created. Note 3: If you open an existing file with FILE_WRITE mode (without combining it with FILE_READ) the data in the file will be deleted and will be empty file. If you want to add (append) data to an existing file you have to open it with the combination of FILE_WRITE and FILE_READ. Note 4: You can not use FILE_READ mode with not existing file. Note 5: You can open more than 32 files from the same program and you can't use the file handles returned from a program with another program. int delimiter: In the case of opening csv the delimiter is the character that separates the data, the default value is ";" delimiter. Example:
FileClose:Syntax:
Description: The FileClose function closes the given file; you pass to it the file handle (you've got this handle when you opened the file with FileOpen function) and it will close it. Parameters: This function takes only one parameter: int handle: The handle of the opened file you want to close. Example:
|