- Examples: File Operations via WinAPI
- How to delete file content?
- Publishing result via FTP
One way is to open file for write and then close it. It will be truncated.
// Create csv file int paramFile = FileOpen("TestFile.csv", FILE_WRITE|FILE_CSV); if(paramFile != INVALID_HANDLE) { // write something FileWrite(paramFile, 1, 2, "three"); FileClose(paramFile); } // truncate file paramFile = FileOpen("TestFile.csv", FILE_WRITE); if(paramFile != INVALID_HANDLE) { FileClose(paramFile); }
First part of above code creates file. Execute it as a script and you'll find "Testfile.csv" with one line.
Second part opens, truncates and closes file.
From the help:
The FILE_WRITE mode of file opening involves that a file will be used for writing in by a program. A try to open a file in this mode results in opening a file of a zero length. Even if there was some information in the file before opening, it will be erased. A try to open a file in this mode can fail, in case the file was previously opened by another program (in the writing mode). |
and without closing and re-opening files?
Why would you do that? You have to touch the file in some way if you want to empty the content.
Alternatively, you can have empty file around and copy it over non-empty.
But that must be done with system commands or through dll.
I believe the objective is ...
1. Create file if none. 2. Open, write something and left it open. 3. While still opened, when something happen, erase the content and write a new one. Still left it open. 4. Repeat step 3. 5. Close if no longer needed.
MQL4 relies on system file handling routines. Compare FileOpen with fopen.
You can do seek and rewrite file with some value, but you cannot shorten the file. Close/open is faster than that.
Search for C++ and c# file functions. Maybe you could import some stream functions that can set stream length to 0 and then do flush to shorten without close/open.
What is the point of this attempt?
Are you trying to prematurely optimize?
Well, the purpose is simple the fastest way to exchange information with other programs.
MT4 - NamedPipe works only with the MT4 owns PipeServer example which does some reads and writes and disconnects. Other program languages (C# or PowerShell) reads and writes to a stream and do hang until the pipe is closed :(
The MT4-Socket examples do not work any more..
Well I think I'll do it with read a file and delete it and write to a file unless it exists.
This is a version for an old fashioned DOS-PC, at least I don't have to write to a floppy disks - and all, that just because Metaquotes moves forward :(
Gooly
Other then mentioned, for interprocess communications on windows there are memory mapped files and SendMessage/PostMessage functions.
You are right, files are worst solution for interprocess communication.
Other then mentioned, for interprocess communications on windows there are memory mapped files and SendMessage/PostMessage functions.
You are right, files are worst solution for interprocess communication.
Can you access the .NET Framework 4.5 from within the MT4? How? Do you know a "Net.dll" that can be imported in an EA or script?
Sorry, can't help you. I've never used .Net framework with MT4.
Try to search for c++ examples for memory mapped files. (here's one on MSDN)
If I remember correctly, memory mapped files are in kernel32.dll
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use