Download MetaTrader 5

delete the content of a file without deleting the file?

To add comments, please log in or register
Your conversation partner speaks foreign language? Automatic translation will help you!
Carl Schreiber
6606
Carl Schreiber 2014.06.13 15:46 
Is there a way to delete the the content of a file (using the MT4-functions!) without deleting the file:  like delete and re-create?
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.06.13 15:54  
What is the current way you done?
Drazen Penic
2544
Drazen Penic 2014.06.13 16:10  

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).
Carl Schreiber
6606
Carl Schreiber 2014.06.13 16:23  

and without closing and re-opening files?

Drazen Penic
2544
Drazen Penic 2014.06.13 18:44  

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. 

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.06.13 20:57  

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.
Drazen Penic
2544
Drazen Penic 2014.06.13 21:23  

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?

Carl Schreiber
6606
Carl Schreiber 2014.06.14 06:46  

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

Drazen Penic
2544
Drazen Penic 2014.06.14 08:54  

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. 

Carl Schreiber
6606
Carl Schreiber 2014.06.14 09:48  
drazen64:

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?
Drazen Penic
2544
Drazen Penic 2014.06.14 13:01  

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 

12
To add comments, please log in or register