Download MetaTrader 5

Reading and writing files anywhere on disk using CreateFileA() etc.

To add comments, please log in or register
JC 2009.07.20 19:49 

There are frequent questions in this forum (e.g. 'Difficulty using FileReadString on file opened outside /experts/files') about reading and writing files anywhere on the hard disk, but there only seems to be one published library for doing so, and it's fairly limited: 'File Operations without Limitations'

The attached include-file uses the CreateFileA(), ReadFile() etc family of Windows API functions rather than _lcreat(), lread() etc. Differences/improvements from are as follows:

  • Provides a read function which uses a 200-byte buffer, whereas seems to read the file 1 byte at a time.
  • Provides a ReadLineFromFile() function which can be used to read CRLF-terminated (or pipe-terminated etc) lines, rather than having to read an entire file into memory and then split it.
  • Provides functions for reading and writing an array of doubles.
  • Provides a wrapper function for opening a new file in append mode.
  • Provides wrapper functions with parameters for opening files in shared mode.
  • The _lcreat() functions etc used in are deprecated by Microsoft, and ideally shouldn't be used.

Use at your own peril; not fully tested etc...

winfile.mqh 25 kb
JC 2009.07.20 19:50  

The attached include-file [...]

...and the attached file is a test script for the above include-file.

fbj 2009.07.20 20:53  

jjc - you're Da Man!

Presently, I do not have requirement but will download and save 'for that rainy day' and the learning about api i/f is always of interest too!

Many thank you's offered :O)


edit: once again having dl'd into ed, I'm struck by your code. Has always proved to be a pleasure to read. Formatting, comments and concise too.

Heaven knows is bad enough coding let alone maintenance - which in my case means after first clean compile, yet your src is agreeable and this is massively important and severely underrated by many...

are you also a 'gun for hire'?

I ask cuz my distinct brain dead feelings seem more obvious these days... like for the last 10yrs :o)

a pm?

JC 2009.07.21 00:58  

jjc - you're Da Man!

Er, no. Leaving the code aside, there are at least two "bugs" just in the comments. The notes on OpenExistingFileForReading() have a nonsensical reference to appending and overwriting, caused by pasting the text from the previous function. And the notes in the test script refer to writing CRLF-terminated lines despite the fact that I changed the code to provide a more useful example of pipe-terminated blocks.

are you also a 'gun for hire'?

Up to a point. See

Do Hung
Do Hung 2014.02.13 01:57  


As MT4 upgrate to build 600, I can not use _lcreat(), lread() etc.

Please support, Thanks!

qjol 2014.02.13 02:44  

according to MSDN - Microsoft:

This function is provided for compatibility with 16-bit versions of Windows. New applications should use the CreateFile function.

B.T.W. in the new MT4 u have to use CreateFileW instead of CreateFileA

Simon Gniadkowski
Simon Gniadkowski 2014.02.13 09:42  


As MT4 upgrate to build 600, I can not use _lcreat(), lread() etc.

Please support, Thanks!

Why are you replying to a 4.5 year old thread ?
lou jiyang
lou jiyang 2015.01.13 05:39  

the newest change is  CreateFileA()---CreateFileW()    DeleteFileA()---DeleteFileW()    delete  StringSplit() function (the new mt4 600+  Contains this function)    

and it's good to use from  Excel2txt2mql   or mql2txt2mql 

and  It should be noted that  the "xxx.txt" must be  unicode encoding

To add comments, please log in or register