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

 

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 https://www.mql5.com/en/code/8314 are as follows:


  • Provides a read function which uses a 200-byte buffer, whereas https://www.mql5.com/en/code/8314 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 https://www.mql5.com/en/code/8314 are deprecated by Microsoft, and ideally shouldn't be used.

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

Files:
winfile.mqh  25 kb
 
jjc:

The attached include-file [...]

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

Files:
 

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?

 
fbj:

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 www.mtintelligence.com/about.aspx

 

Hi,

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

Please support, Thanks!

 

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

 
Dohung:

Hi,

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 ?
 

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

Reason: