Download MetaTrader 5

Cannot open CSV in a common place

To add comments, please log in or register
Marcelo Coutinho
1201
Marcelo Coutinho  

Good Afternoon !

Can someone please give a clue of the problem I am facing open the CSV file outside the regular MQL5 structure ?

filename returns C:\Users\Administrator\AppData\Roaming\MetaQuotes\Terminal\Common\Files\youbotg9.csv

fileHandle returns -1

Thank you very much

string commondata_path = TerminalInfoString(TERMINAL_COMMONDATA_PATH);
string filename = commondata_path + "\\Files\\youbotg9.csv";
Print(filename);

int  fileHandle=FileOpen(filename,FILE_COMMON|FILE_READ|FILE_WRITE|FILE_CSV,",");
Print(fileHandle);
FileSeek(fileHandle,0,SEEK_END);
FileWrite(fileHandle,"OFF");
FileClose(fileHandle);
Irwan Adnan
923
Irwan Adnan  
YouTrade:

Good Afternoon !

Can someone please give a clue of the problem I am facing open the CSV file outside the regular MQL5 structure ?

filename returns C:\Users\Administrator\AppData\Roaming\MetaQuotes\Terminal\Common\Files\youbotg9.csv

fileHandle returns -1

Thank you very much

dont use whole addres, just use filename instead. Replace with this :

 

string filename = youbotg9.csv";
Marcelo Coutinho
1201
Marcelo Coutinho  
belido:

dont use whole addres, just use filename instead. Replace with this :

 

Belido,

Thank you for answering.

If I only use the filename, it will work but will save the file @ the regular MQL5 Terminal structure.

I need to save it in a public location since more MT5 instances will be looking for it.

Irwan Adnan
923
Irwan Adnan  
YouTrade:

Belido,

Thank you for answering.

If I only use the filename, it will work but will save the file @ the regular MQL5 Terminal structure.

I need to save it in a public location since more MT5 instances will be looking for it.

If you dont include the folder name, since you use |FILE_COMMON tag, it WILL write in  C:\Users\Administrator\AppData\Roaming\MetaQuotes\Terminal\Common\Files\youbotg9.csv as your previous question. If I am not wrong, it wont write other then the common folder or MT5 folder itself.
Marcelo Coutinho
1201
Marcelo Coutinho  
belido:
If you dont include the folder name, since you use |FILE_COMMON tag, it WILL write in  C:\Users\Administrator\AppData\Roaming\MetaQuotes\Terminal\Common\Files\youbotg9.csv as your previous question. If I am not wrong, it wont write other then the common folder or MT5 folder itself.
Checking ...
Marcelo Coutinho
1201
Marcelo Coutinho  

Well ... Please accept my apologies but sometimes it does not work.

Two situations are happening now:

(1)   When file does not exist, it gives me an error 5004 (File not found). I understand FileOpen() creates a file when it does not exist.

(2)   When I manually create the file, script runs with no problems but writes nothing in the file. 

When you run @ the regular MQL5 Files folder, it runs perfectly. 

   int fileHandle=FileOpen("youbotg9.csv",FILE_COMMON|FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_CSV,",");

   if(fileHandle==INVALID_HANDLE)
     {
      Print("INVALID HANDLE: ",GetLastError());
      ResetLastError();
     }
   else
     {
      FileSeek(fileHandle,0,SEEK_END);
      FileWrite(fileHandle,"OFF");
      FileClose(fileHandle);
      Print("FILE CREATED OFF");
     }

Irwan Adnan
923
Irwan Adnan  
YouTrade:

Well ... Please accept my apologies but sometimes it does not work.

Two situations are happening now:

(1)   When file does not exist, it gives me an error 5004 (File not found). I understand FileOpen() creates a file when it does not exist.

(2)   When I manually create the file, script runs with no problems but writes nothing in the file. 

When you run @ the regular MQL5 Files folder, it runs perfectly. 

No problem

Perhaps you miss |FILE_WRITE and/or |FILE_READ tag. Just add this tag. It is not same with FILE_SHARE.

Marcelo Coutinho
1201
Marcelo Coutinho  
belido:

No problem

Perhaps you miss |FILE_WRITE and/or |FILE_READ tag. Just add this tag. It is not same with FILE_SHARE.

Working now !

The only thing that is not clear to me is keeping the file CSV file open while running the script to add ON and OFF makes no difference.

I understood we need FILE_SHARE to read/write the csv file to avoid getting an error while another instance of MT5 is using it.

Anyway ... You gave me a good start ... I will test tomorrow running with some MT5 instances to understand the difference between FILE and FILE_SHARE

Thank you very much for your attention.

Regards 

Irwan Adnan
923
Irwan Adnan  
YouTrade:

Working now !

The only thing that is not clear to me is keeping the file CSV file open while running the script to add ON and OFF makes no difference.

I understood we need FILE_SHARE to read/write the csv file to avoid getting an error while another instance of MT5 is using it.

Anyway ... You gave me a good start ... I will test tomorrow running with some MT5 instances to understand the difference between FILE and FILE_SHARE

Thank you very much for your attention.

Regards 

You are welcome. I am also still learing MQL part that interesting to me. I also ask here when I got stuck.
To add comments, please log in or register