Download MetaTrader 5
To add comments, please log in or register
Looking for like-minded people? You will find lots of them on the Forum!
Rusty G. Edwards
12
Rusty G. Edwards 2014.03.15 06:19 

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

Alain Verleyen
Moderator
28702
Alain Verleyen 2014.03.15 07:57  

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.

Rusty G. Edwards
12
Rusty G. Edwards 2014.03.15 18:10  
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

whroeder1
13656
whroeder1 2014.03.15 18:49  
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);
Rusty G. Edwards
12
Rusty G. Edwards 2014.03.16 04:07  
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.

/
To add comments, please log in or register