MQL4: Sharing file between two MT4 on the same PC

 

I've tried to FileOpen() a single file (in the common folder) in two separate MT4 instances, but when the file is opened by the first one, the second one cannot open it and -1 is returned by FileOpen().

Used flags on both functions: FILE_READ|FILE_COMMON

Actually, I want to keep the file open for writing in one of them, and keep it open for reading in the other. I just started to test by FILE_READ for now.

 
mohammad:

I've tried to FileOpen() a single file (in the common folder) in two separate MT4 instances, but when the file is opened by the first one, the second one cannot open it and -1 is returned by FileOpen().

Used flags on both functions: FILE_READ|FILE_COMMON

Actually, I want to keep the file open for writing in one of them, and keep it open for reading in the other. I just started to test by FILE_READ for now.

You have to use FILE_SHARE_READ and/or FILE_SHARE_WRITE.
 
First script keeps "file.csv" open for reading. Second scripts tries to open the file and modify it; but returns error 5004: file cannot be opened


void OnStart()
  {
   int handle=FileOpen("file.csv",FILE_READ|FILE_SHARE_READ|FILE_COMMON);

   while(!IsStopped())
     {
      if(FileIsEnding(handle))
        {
         Sleep(5000);
         continue;
        }
      Print("Integer = ",FileReadNumber(handle));
      Sleep(5000);
     }
   FileClose(handle);
  }
#property show_inputs
input int number=0;

void OnStart()
  {
   int handle=FileOpen("file.csv",FILE_READ|FILE_SHARE_READ|FILE_WRITE|FILE_COMMON);
   if(handle==INVALID_HANDLE) 
     {
      Print(_LastError," ,FileOpen ",handle);
      return;
     }
//add new number to the end of the file
   if(FileSeek(handle,0,SEEK_END))
      FileWrite(handle,number);
   FileClose(handle);
  }
 
mohammad:
First script keeps "file.csv" open for reading. Second scripts tries to open the file and modify it; but returns error 5004: file cannot be opened


Of course, your first script open the file for reading. You can't then open it for writing with an other script.

If a file is open for reading you can't open it for writing from an other code, and if open for writing you can't open it for reading only.

 
Depending on how much info is in the file, you could try using globals or named pipes
 
Alain Verleyen:

Of course, your first script open the file for reading. You can't then open it for writing with an other script.

If a file is open for reading you can't open it for writing from an other code, and if open for writing you can't open it for reading only.

That means two scripts only can either share reading or share writing? (FILE_SHARE_READ & FILE_SHARE_WRITE)


p.s: sorry Stuart "named pipes" method is too complicated to me, I've already seen that :)

 
mohammad:

p.s: sorry Stuart "named pipes" method is too complicated to me, I've already seen that :)

It's really not that complicated. Do a bit of a google and you will find some pretty simple examples you can expand on. But up to you mate :)
 
mohammad:

That means two scripts only can either share reading or share writing? (FILE_SHARE_READ & FILE_SHARE_WRITE)


See the table on this page to know which access mode/sharing mode combination are allowed.
Creating and Opening Files (Windows)
  • msdn.microsoft.com
The CreateFile function can create a new file or open an existing file. You must specify the file name, creation instructions, and other attributes. When an application creates a new file, the operating system adds it to the specified directory. The operating system assigns a unique identifier, called a handle, to each file that is opened or...
 
Alain Verleyen:
See the table on this page to know which access mode/sharing mode combination are allowed.

Superb help! But still some confusion.

According to the table (second row), if the first call has these flags: GENERIC_READ, FILE_SHARE_WRITE, then the second call is valid to have GENERIC_WRITE access. I tried this combination in my code, but again failed.

 

This combination worked:

First Call: FILE_READ|FILE_SHARE_READ|FILE_SHARE_WRITE

Second call: FILE_WRITE|FILE_SHARE_READ

Thanks for your great help Alain.

Reason: