In Part 60 we continued our discussion of the WriteFile() function. We’ll be wrapping that up in this installment.
We talked about the FileWritexx() functions and how to use them to write each individual variable to your file. All that’s left now is to FileClose() your file. Again, don’t forget to close your file as MT4 limits the total number of files you can have open at one time and each time your forget to close a file, it is counted toward that total. If you call the WriteFile() function as much as I do, your EA will soon crash with too many open files.
After closing the file, we set the variable Success to true to be returned to the calling program – indicating the successful completion of the WriteFile() function. As I mentioned earlier, if your ReadFile() function returned an error while opening or the DFVersion number in the file didn’t agree with the code’s DataFileVersion variable – suggesting an old, incompatible data file – the WriteFile() function may have been called from the ReadFile() function. In that case, a successful return from WriteFile() to ReadFile() will cause the Success variable in ReadFile() to be set and will return a successful completion to that calling function. Whew! That’s convoluted. It may be obvious to some readers, but for those who don’t quite get it, here’s the code to which I’m referring:
FileClose(h); Success = true; } //if(DFVersion == DataFileVersion) else Success = WriteFile(); } //if(h != -1) else Success = WriteFile(); return(Success);
This is the last few lines of the ReadFile() function. Notice that if h == -1 or the DFVersion != DataFileVersion, we use the line Success = WriteFile(). That sets the variable Success in ReadFile() to the returned result of WriteFile(). So a successful conclusion of WriteFile() results in a successful conclusion of ReadFile().
While we’re here, I may have not covered the simple, but unconventional notation that is used in MQL4. It comes straight from the C programming language, so I’ve been doing it for so long, I no longer think about it. When testing equalities using the if() function, MQL4 uses the double equal sign (==) to test for “is equal to” and the exclamation point/equal sign (!=) to test for “is not equal to”. So the following:
if(h != -1)
is the equivalent of saying “If h is not equal to -1”. And
if(DFVersion == DataFileVersion)
is the equivalent of saying “If DFVersion is equal to DataFileVersion)”.
I hope that clears it up for anyone that may have been confused. Please don’t hesitate to ask questions if you have any.
That wraps our discussion of the ReadFile() and WriteFile() functions. We’ll be discussing file handling more in a future installment, but in the next article we’ll be discussing Event Detection in MT4.
Thanks for your attention and please follow me on Twitter and LinkedIn.
Latest posts by admin (see all)
- Forex Trading in Summertime, what the charts can tell you. - September 22, 2017
- The Secret of Taking Profit and Why it is Important - September 20, 2017
- Fading the Momentum in Forex Trading - September 14, 2017
Winner’s Edge Trading, as seen on: