Download MetaTrader 5

How to the id:5002 problem of FileOpen?

To add comments, please log in or register
manwind ko
132
manwind ko 2012.07.02 14:00 

 My Code as follow:

int OnInit()
  {
//---
string filename="D:\\MYDATA.CSV";
 
    int filehandle=FileOpen(filename,FILE_READ|FILE_WRITE|FILE_CSV|FILE_ANSI,',');
   if(filehandle<0)
     {
      Print("Failed to open the file by the absolute path ",filename);
      Print("Error code ",GetLastError());
     }
      ResetLastError();
      FileWrite(filehandle,"1111111");
      FileClose(filehandle);
//---
   return(0);
  }

The tester report :

19:56:18 2012.01.02 00:00:00   Failed to open the file by the absolute path D:\MYDATA.CSV                                                       
19:56:18 2012.01.02 00:00:00   Error code 5002                                                                                                  
19:56:18 final balance 10000.00                                                                                                                 
19:56:18 OnTester result 0                                                                                                                       
19:56:18 log file "C:\Users\VC\AppData\Roaming\MetaQuotes\Tester\E6E3D0917DD641581E4779524EB3B1AA\Agent-127.0.0.1-3000\logs\20120702.log" written
19:56:18 connection closed            

                                                                                                           
How to the id:5002 problem of FileOpen? I've tried to many methods.  If  anyone  tell me how to deal it, I'm very  appreciated to you.

OOP in MQL5 by Example: Processing Warning and Error Codes
  • 2010.05.26
  • KlimMalgin
  • www.mql5.com
The article describes an example of creating a class for working with the trade server return codes and all the errors that occur during the MQL-program run. Read the article, and you will learn how to work with classes and objects in MQL5. At the same time, this is a convenient tool for handling errors; and you can further change this tool according to your specific needs.
onewithzachy
2271
onewithzachy 2012.07.02 14:45  
manwind:

My Code as follow:

int OnInit()
  {
//---
string filename="D:\\MYDATA.CSV";
  
    int filehandle=FileOpen(filename,FILE_READ|FILE_WRITE|FILE_CSV|FILE_ANSI,',');
   if(filehandle<0)
     {
      Print("Failed to open the file by the absolute path ",filename);
      Print("Error code ",GetLastError());
     }
      ResetLastError();
      FileWrite(filehandle,"1111111");
      FileClose(filehandle); 
//---
   return(0);
  }

The tester report :

19:56:18 2012.01.02 00:00:00   Failed to open the file by the absolute path D:\MYDATA.CSV                                                        
19:56:18 2012.01.02 00:00:00   Error code 5002                                                                                                   
19:56:18 final balance 10000.00                                                                                                                  
19:56:18 OnTester result 0                                                                                                                       
19:56:18 log file "C:\Users\VC\AppData\Roaming\MetaQuotes\Tester\E6E3D0917DD641581E4779524EB3B1AA\Agent-127.0.0.1-3000\logs\20120702.log" written
19:56:18 connection closed            

                                                                                                           
How to the id:5002 problem of FileOpen? I've tried to many methods.  If  anyone  tell me how to deal it, I'm very  appreciated to you.

Hi manwind,

1. Please use SRC button to post your code.

 

2. Error 5002 is Error file name or invalid file name, that is because you file is in wrong place. This is the correct place to put your file (click here).

3. I wrote some file operation back then, you may want to see it - click here. And if you don't mind, I correct a little of your codes. As long as you put your file in the right place, there should be no problem

int OnInit()
  {
//---
   string filename="MYDATA.CSV";
   ResetLastError();
   int filehandle=FileOpen(filename,FILE_READ|FILE_WRITE|FILE_CSV|FILE_ANSI,',');
   if(filehandle<0)
      {
      Print("Failed to open the file by the absolute path ",filename);
      Print("Error code ",GetLastError());
      }
      else
      {
      FileWrite(filehandle,"1111111");
      FileClose(filehandle);
      }
     
//---
   return(0);
  }

 :D

manwind ko
132
manwind ko 2012.07.03 09:26  
onewithzachy:

Hi manwind,

1. Please use SRC button to post your code.

 

2. Error 5002 is Error file name or invalid file name, that is because you file is in wrong place. This is the correct place to put your file (click here).

3. I wrote some file operation back then, you may want to see it - click here. And if you don't mind, I correct a little of your codes. As long as you put your file in the right place, there should be no problem

 :D

Hi onewithzachy:

According to your code, I modified mine.Now data has correctly written in files. Thank you for your suggestion!

 Now I fall in new questions.

1. The default file save path is too long in my pc. it is C:\Users\VC\AppData\Roaming\MetaQuotes\Tester\E6E3D0917DD641581E4779524EB3B1AA\Agent-127.0.0.1-3000\MQL5. Could I modify the default path?

2.If I modify the path, for example

string filename="C:\\MQL5\\MYDATA.CSV";
fp=FileOpen(filename,FILE_READ|FILE_WRITE|FILE_CSV,';');
  if(fp==INVALID_HANDLE)
{
    Print("not find "+filename+" !",GetLastError());
}
else
{
  FileSeek(fp,0,SEEK_END);
  FileWrite(fp,"11111111");
}
FileClose(fp)

the tester still report id:5002 error:

  15:23:37 2012.01.02 03:04:00   not find C:\MQL5\MYDATA.CSV !5002

Why? Thank a lot in advance for any help.

onewithzachy
2271
onewithzachy 2012.07.03 09:56  
manwind:

Hi onewithzachy:

According to your code, I modified mine.Now data has correctly written in files. Thank you for your suggestion!

 Now I fall in new questions.

1. The default file save path is too long in my pc. it is C:\Users\VC\AppData\Roaming\MetaQuotes\Tester\E6E3D0917DD641581E4779524EB3B1AA\Agent-127.0.0.1-3000\MQL5. Could I modify the default path?

2.If I modify the path, for example

the tester still report id:5002 error:

  15:23:37 2012.01.02 03:04:00   not find C:\MQL5\MYDATA.CSV !5002

Why? Thank a lot in advance for any help.

Hi manwind,

1. Me too have that long data path, it's TerminalInfoString(TERMINAL_DATA_PATH)); (click here for example). Actually we don't have to write it's long data path, all we have to do is write 

 string filename="MYDATA.CSV";
 //-->> which is the same with 
 string filename=TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL5\\Files\\"+"MYDATA.CSV";

and to find the file, from MT5 > click File > click Open Data Folder, and from MetaEditor > click File > click Open Data Folder or Open Common Data Folder, and there we have 'em. BTW, if its only csv or txt files, you can also open them right inside MetaEditor, though it's not quickly refreshed if there's new data written in.

2. Unfortunately we can not modify the default location for file operation in MT5 :(, because that is the working folder for MQL5 file operations - that's the place where file operation goes to works.

Though you may interested in reading this article which is written by MetaQuotes in MQL4.com, on how to work with file in any location by not using MQL command but by using Win API. I haven't try with tester yet (click here and click here, please).

:D 

File Operations via WinAPI - MQL4 Articles
  • www.mql5.com
File Operations via WinAPI - MQL4 Articles: examples of expert advisors and strategy tester
manwind ko
132
manwind ko 2012.07.03 11:07  

Hi  onewithzachy:

     Thanks! I finally know them. You're  experienced. Thank you again for  your help!

jackprobe
26
jackprobe 2014.05.24 13:12  

Hi all,

I am having the same problem with MT4 build 6xx. Below is the code:

   string   terminal_data_path   = TerminalInfoString(TERMINAL_DATA_PATH);
   string   inputFile = terminal_data_path+"\\MQL4\\Files\\"+"testing.csv";    
   
   //---
   Comment("Trying to open file for reading : "+inputFile); 
   Print("Trying to open file for reading : "+inputFile); 
   handle   = FileOpen(inputFile,FILE_READ|FILE_CSV);
   if (handle<0) {
      Print("Failed to open file: "+inputFile); 
      Print("Error code ",GetLastError());
      Comment("\nError reading file : "+inputFile); 
      return(0); 
   }

Actually the "testing.csv" file is there (please see image).  But i always get "5002" or file not found error ...

Why ?

Thanks

Alain Verleyen
Moderator
31137
Alain Verleyen 2014.05.24 16:13  
jackprobe:

Hi all,

I am having the same problem with MT4 build 6xx. Below is the code:

Actually the "testing.csv" file is there (please see image).  But i always get "5002" or file not found error ...

Why ?

Thanks

No need to specify the path :

   string   terminal_data_path   = TerminalInfoString(TERMINAL_DATA_PATH);
   string   inputFile = terminal_data_path+"\\MQL4\\Files\\"+"testing.csv";    
   
   //---
   Comment("Trying to open file for reading : "+inputFile); 
   Print("Trying to open file for reading : "+inputFile); 
   handle   = FileOpen(inputFile,FILE_READ|FILE_CSV);
   if (handle<0) {
      Print("Failed to open file: "+inputFile); 
      Print("Error code ",GetLastError());
      Comment("\nError reading file : "+inputFile); 
      return(0); 
   }
jackprobe
26
jackprobe 2014.05.25 03:20  
angevoyageur:

No need to specify the path :

Hi angevoyageur,

Great, it works .....

Thanks
-Jack

Marcelo Coutinho
1158
Marcelo Coutinho 2014.11.16 15:47  

Good Afternoon

I have just posted another topic closed to tis one to I will keep it here in order to keep the topic organized.

On MQL5 documentation, there is a Common file folder where several instances on MT5 can read/write to a file.

I am using the structure below but unfortunately it is not working.

Anyone have any experience with Common_Data_Path ?

Thank you ! 

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);

filename returns C:\Users\Administrator\AppData\Roaming\MetaQuotes\Terminal\Common\Files\youbotg9.csv
fileHandle returns -1
Irwan Adnan
925
Irwan Adnan 2014.11.16 16:11  
YouTrade:

Good Afternoon

I have just posted another topic closed to tis one to I will keep it here in order to keep the topic organized.

On MQL5 documentation, there is a Common file folder where several instances on MT5 can read/write to a file.

I am using the structure below but unfortunately it is not working.

Anyone have any experience with Common_Data_Path ?

Thank you ! 

did you see angevoyageur post?
Alain Verleyen
Moderator
31137
Alain Verleyen 2014.11.17 18:29  
YouTrade:

Good Afternoon

I have just posted another topic closed to tis one to I will keep it here in order to keep the topic organized.

On MQL5 documentation, there is a Common file folder where several instances on MT5 can read/write to a file.

I am using the structure below but unfortunately it is not working.

Anyone have any experience with Common_Data_Path ?

Thank you ! 

Once again :

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);
To add comments, please log in or register