Download MetaTrader 5

Error opening the file in a specific directory

To add comments, please log in or register
noorad sharafi
1702
noorad sharafi 2013.11.13 06:51 

hi

I just want the folder to be opened c:\

Thank you ;)

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {

  
      string filename="c:\test.csv";

   int filehandle=FileOpen(filename,FILE_WRITE|FILE_CSV);
   if(filehandle<0)
     {
      Print("Failed to open ");
      Print("Error code ",GetLastError());
     }
 

   ResetLastError();

   if(filehandle!=INVALID_HANDLE)
     {
      FileWrite(filehandle,TimeCurrent(),Symbol(),PERIOD_CURRENT);
      FileClose(filehandle);
      Print("FileOpen OK");
     }
 
  }
enbo lu
Moderator
1951
enbo lu 2013.11.13 07:08  

Note

For security reasons, work with files is strictly controlled in the MQL5 language. Files with which file operations are conducted using MQL5 means, cannot be outside the file sandbox.

The file is opened in the folder of the client terminal in the subfolder MQL5\files (or testing_agent_directory\MQL5\files in case of testing). If FILE_COMMON is specified among flags, the file is opened in a shared folder for all MetaTrader 5 client terminals.

"Named pipes" can be opened according to the following rules:

Pipe name is a string, which should have the following look: "\\servername\pipe\pipename", where servername - server name in the network, while pipename is a pipe name. If the pipes are used on the same computer, the server name can be omitted but a point should be inserted instead of it: "\\.\pipe\pipename". A client trying to connect the pipe should know its name.

FileFlush() and FileSeek() should be called to the beginning of a file between sequential operations of reading from the pipe and writing to it.

A special symbol '\' is used in shown strings. Therefore, '\' should be doubled when writing a name in MQL5 application. It means that the above example should have the following look in the code: "\\\\servername\\pipe\\pipename".

noorad sharafi
1702
noorad sharafi 2013.11.13 09:24  
luenbo:

Note

For security reasons, work with files is strictly controlled in the MQL5 language. Files with which file operations are conducted using MQL5 means, cannot be outside the file sandbox.

The file is opened in the folder of the client terminal in the subfolder MQL5\files (or testing_agent_directory\MQL5\files in case of testing). If FILE_COMMON is specified among flags, the file is opened in a shared folder for all MetaTrader 5 client terminals.

"Named pipes" can be opened according to the following rules:

Pipe name is a string, which should have the following look: "\\servername\pipe\pipename", where servername - server name in the network, while pipename is a pipe name. If the pipes are used on the same computer, the server name can be omitted but a point should be inserted instead of it: "\\.\pipe\pipename". A client trying to connect the pipe should know its name.

FileFlush() and FileSeek() should be called to the beginning of a file between sequential operations of reading from the pipe and writing to it.

A special symbol '\' is used in shown strings. Therefore, '\' should be doubled when writing a name in MQL5 application. It means that the above example should have the following look in the code: "\\\\servername\\pipe\\pipename".

thnaks for replay

Can you please help me modify my code

;)

Christian Beckmann
2343
Christian Beckmann 2013.11.13 09:56  
noorad2000:

thnaks for replay

Can you please help me modify my code

;)

try 
string filename="c:\\test.csv";
Alain Verleyen
Moderator
31085
Alain Verleyen 2013.11.13 09:59  
b3ck1:
try 

Did you read the reply of luenbo ? YOU CAN'T OPEN FILE outside "the subfolder MQL5\files (or testing_agent_directory\MQL5\files in case of testing). If FILE_COMMON is specified among flags, the file is opened in a shared folder for all MetaTrader 5 client terminals."

Documentation on MQL5: Standard Constants, Enumerations and Structures / Input/Output Constants / File Opening Flags
Documentation on MQL5: Standard Constants, Enumerations and Structures / Input/Output Constants / File Opening Flags
  • www.mql5.com
Standard Constants, Enumerations and Structures / Input/Output Constants / File Opening Flags - Documentation on MQL5
noorad sharafi
1702
noorad sharafi 2013.11.13 10:06  
angevoyageur:

Did you read the reply of luenbo ? YOU CAN'T OPEN FILE outside "the subfolder MQL5\files (or testing_agent_directory\MQL5\files in case of testing). If FILE_COMMON is specified among flags, the file is opened in a shared folder for all MetaTrader 5 client terminals."

Yes I read it, but he said that I could not do method
noorad sharafi
1702
noorad sharafi 2013.11.13 10:10  

I use this trick but I could not

#import "kernel32.dll"
 bool CopyFileA(string lpExistingFileName, string lpNewFileName, bool failIfExists); 
#import
....

..
CopyFileA ("test.csv","c:\\"test.csv", true  )
Simon Gniadkowski
Moderator
18002
Simon Gniadkowski 2013.11.13 10:12  
noorad2000:
Yes I read it, but he said that I could not do method
I quoted your post  ^ ^ ^ ^ ^  so you know I am replying to you,  angevoyageur   quoted the post from  b3ck1  not from you so his reply is meant for  b3ck1   
Alain Verleyen
Moderator
31085
Alain Verleyen 2013.11.13 10:17  
RaptorUK:
I quoted your post  ^ ^ ^ ^ ^  so you know I am replying to you,  angevoyageur   quoted the post from  b3ck1  not from you so his reply is meant for  b3ck1   
Exactly
Alain Verleyen
Moderator
31085
Alain Verleyen 2013.11.13 10:19  
noorad2000:

I use this trick but I could not

Why do you need to do that ? It's a bad idea to bypass security when you do not even understand why it exists.

The best solution, if you really need to open file outside MQL5 folders, it to use named pipe : Communicating With MetaTrader 5 Using Named Pipes Without Using DLLs

noorad sharafi
1702
noorad sharafi 2013.11.13 10:41  
angevoyageur:

Why do you need to do that ? It's a bad idea to bypass security when you do not even understand why it exists.

The best solution, if you really need to open file outside MQL5 folders, it to use named pipe : Communicating With MetaTrader 5 Using Named Pipes Without Using DLLs

Thank Alain 

But I do not want to send files between the two programs.

12
To add comments, please log in or register