FileOpen locks the file for access by another programme

 

I am writing mql script to look for any change in the file and if it detectes any change in the file then it performs some trading function

But after the mql "FileOpen "call desired file becomes unusable by other application even with "FILE_SHARE_READ" flag.

What I am doing wrong or this is how Mql FileOpen works.

Here is my script

void OnStart()
  {
//---
   int filehandle=FileOpen("sample.csv",FILE_SHARE_READ);
   if(filehandle!=INVALID_HANDLE)
     {
        FileSeek(filehandle,0,SEEK_END);
        while(true)
         {
            main(filehandle);  
            Sleep(Timer);
         }
     }else
     {
     
     if(GetLastError()==4103){ 
      Print("Can not open signal file,please verify it,s location at "+SignalCSVFile+" and restart the script.",GetLastError());
      PlaySound("Bzrrr.wav"); 
     }else{
     Print("Error while opening file at location "+SignalCSVFile+" and restart the script.",GetLastError());
     }
     }
  }
  
  void main(int fileHandle)
{
   FileContent(fileHandle);          
}

  void FileContent(int fileHandle)
{
if(StringLen(FileReadString(fileHandle))>0){
   Print("FileContent "+ FileReadString(fileHandle));
}

Thank you for your help

 

Welcome to mql4.com forum,

From the documentation we can read about FILE_SHARE_READ :

Shared access for reading from several programs. Flag is used in FileOpen(), but it does not replace the necessity to indicate FILE_WRITE and/or the FILE_READ flag when opening a file.

 
angevoyageur:

Welcome to mql4.com forum,

From the documentation we can read about FILE_SHARE_READ :



I used the following combination of mode_flag

int filehandle=FileOpen("abc.csv",FILE_SHARE_READ|FILE_CSV)
int filehandle=FileOpen("abc.csv",FILE_READ|FILE_SHARE_READ|FILE_CSV)
int filehandle=FileOpen("abc.csv",FILE_SHARE_READ|FILE_READ)

But still facing the same problem of file being locked down.

Thank You

 
You open the file, but don't say what type or for which direction.
   int filehandle=FileOpen("sample.csv",FILE_SHARE_READ);
Try FILE_READ|FILE_SHARE_READ|FILE_CSV
Then you seek to the end of the file.
        FileSeek(filehandle,0,SEEK_END);
Why are you seeking?
You then try to read past the end of the file, which gets nothing (because you are at the END) (and even if it did, you throw that string away,) then the IF does nothing.
if(StringLen(FileReadString(fileHandle))>0){
   Print("FileContent "+ FileReadString(fileHandle));
Perhaps you want something like:
string data = FileReadString(fileHandle); 
if(StringLen(data)>0){
   Print("FileContent "+ data);
 
WHRoeder:
You open the file, but don't say what type or for which direction. Try FILE_READ|FILE_SHARE_READ|FILE_CSV
Then you seek to the end of the file. Why are you seeking?
You then try to read past the end of the file, which gets nothing (because you are at the END) (and even if it did, you throw that string away,) then the IF does nothing.
Perhaps you want something like:


The purpose of this script is to detect any change(Addition of new csv rows) in the csv file so at the start of script it move to end of file and in each timer tick script sees that if there is any new data added.

My problem is that the file is LOCKED and can not be edited by other process unless I CloseHandle or stop the script.

 
test on EURUSD,H1 there are no trading operations test on NZDUSD,H1 there are no trading operations test on GBPUSDcheck,M30 strategy tester report 109 total trades test on XAUUSDcheck,Daily there are no trading operations
Reason: