cannot open file ..

 

Hi everybody,


After spending hours and try every options of files type and so on, I still cannot find why I cannot open a file, even in the MQL5 folder or the Tester folder. I use the basic code ..

   string filename="Parametres.txt";
   int hdle=FileOpen(filename,FILE_READ|FILE_TXT|FILE_ANSI,0);  //
   if(hdle<0){int err=GetLastError();
   Print("Failed to open the file ",err);
   }

I tried both TXT and CSV files typr, nothing work, always get the 5004 error.

I had never had any trouble with the FILE_WRITE function which work perfectly; Only with FILE_READ so far I have trouble.

I precise that I am looking for the Tester right location for now.. Seems not working at all in the Tester/files folder

Dont find a solution

Thanks for your help ..

Documentation on MQL5: Constants, Enumerations and Structures / Input/Output Constants / File Opening Flags
Documentation on MQL5: Constants, Enumerations and Structures / Input/Output Constants / File Opening Flags
  • www.mql5.com
File Opening Flags - Input/Output Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the file.
     How To Ask Questions The Smart Way. (2004)
          Be precise and informative about your problem

No problem with your write code, which you don't show.

Did you guess at the location or did you open the «DataFolder»/files. Did you show us?

 

Hi William, thanks for reply. Sorry to not be so clear. I thought the pb I have is a very common pb which you already must have had many time questions for it.

Whith this short code, I should be able to open the file "Parametres.csv" located in the "File" subfolder of the "Tester" folder located in the MT5 terminal folder

see attached file


   string filename="Parametres.txt";
   int hdle=FileOpen(filename,FILE_READ|FILE_TXT|FILE_ANSI,0);  //
   
   if(hdle<0){int err=GetLastError();
     Print("Failed to open the file ",err);
   }
Files:
MT5path.JPG  67 kb
 
Volcanbleu #: I should be able to open the file "Parametres.csv" located in the "File" subfolder of the "Tester" folder located in the MT5 terminal folder
  1. Your code isn't trying to open "Parametres.csv"
  2. Your screenshot isn't the "Tester" folder.
  3. You still haven't shown your write code. Did you close the handle?
 

Yes, I tried both, MQL5 folder and Tester folder.

Yes I close the handle / file after opening

void PARAM(){
   Print("Dossier où se trouvent les données du terminal :\n"+TerminalInfoString(TERMINAL_DATA_PATH));
   //string filename=TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL5\\Files\\"+"fichierminutes.csv";
   string filename="fichiertest.csv";
   int hdle=FileOpen(filename,FILE_READ|FILE_CSV|FILE_ANSI,0);  //
   if(hdle<0){int err=GetLastError();
   Print("Failed to open the file ",err);
   }
   
   ulong size=FileSize(hdle);
   for (int P=1;P<nbpair+1;P++){
        // reading the file
   } 
   FileClose(hdle);
}
 
Look at the log and tell us what is the error!
 

I retried with a brand new csv file named "test.csv" created with Excel using the command "Save as csv file(separator ; ) to be sure the file was not already open.

Made sure that the file is in the "Tester/files" folder

I have the same error 5004. (I am testing it with the debogger).

void PARAM(){
   Print("Dossier où se trouvent les données du terminal :\n"+TerminalInfoString(TERMINAL_DATA_PATH));
   //string filename=TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL5\\Files\\"+"fichierminutes.csv";
   string filename="test.csv";
   int hdle=FileOpen(filename,FILE_READ|FILE_CSV|FILE_ANSI,0);  //
   if(hdle<0){int err=GetLastError();
   Print("Failed to open the file ",err);
   }
   
   ulong size=FileSize(hdle);
   //struct par{};
   //double par[];
   //long count=FileLoad(filename,par);
   for (int P=1;P<nbpair+1;P++){
      //string ligne=FileReadDouble(hdle);  
      //string resu[]; 
      //string sep=" "; ushort u_sep; u_sep=StringGetCharacter(sep,0); 
      //int k=StringSplit(ligne,u_sep,resu);
      //ArrayPrint(resu);
      //z[P].mnbase=FileReadInteger(hdle); z[P].rangeper=FileReadInteger(hdle); z[P].atrper=FileReadInteger(hdle); z[P].profcf=FileReadInteger(hdle);  z[P].stopcf=FileReadInteger(hdle);z[P].tendmn=FileReadInteger(hdle); z[P].moysec=FileReadInteger(hdle);z[P].moypts=FileReadInteger(hdle); z[P].hdeb=FileReadInteger(hdle);
   } 
   FileClose(hdle);
}

if I put the csv file in the TERMINAL_DATA_PATH I get the 5002 error code instead of 5004


void PARAM(){
   Print("Dossier où se trouvent les données du terminal :\n"+TerminalInfoString(TERMINAL_DATA_PATH));
   //string filename=TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL5\\Files\\"+"fichierminutes.csv";
   string filename=TerminalInfoString(TERMINAL_DATA_PATH)+"\\test.csv";
   int hdle=FileOpen(filename,FILE_READ|FILE_CSV|FILE_ANSI,0);  //
   if(hdle<0){int err=GetLastError();
      Print("Failed to open the file ",err);
   }
   else   ulong size=FileSize(hdle);
   
 
   //struct par{};
   //double par[];
   //long count=FileLoad(filename,par);
   for (int P=1;P<nbpair+1;P++){
      //string ligne=FileReadDouble(hdle);  
      //string resu[]; 
      //string sep=" "; ushort u_sep; u_sep=StringGetCharacter(sep,0); 
      //int k=StringSplit(ligne,u_sep,resu);
      //ArrayPrint(resu);
      //z[P].mnbase=FileReadInteger(hdle); z[P].rangeper=FileReadInteger(hdle); z[P].atrper=FileReadInteger(hdle); z[P].profcf=FileReadInteger(hdle);  z[P].stopcf=FileReadInteger(hdle);z[P].tendmn=FileReadInteger(hdle); z[P].moysec=FileReadInteger(hdle);z[P].moypts=FileReadInteger(hdle); z[P].hdeb=FileReadInteger(hdle);
   } 
   FileClose(hdle);
}

here is the log of the last attempt

MD    0    09:22:42.719    Tester      atrgloper=10
DD    0    09:22:42.719    Tester      lpos1=13959039
KQ    0    09:22:42.733    Ticks    EURUSDdu : real ticks begin from 2017.08.07 00:00:00
LJ    0    09:22:42.767    KryptoMulti9 (EURUSDdu,M1)    2022.02.27 22:00:00   Dossier où se trouvent les données du terminal :
QJ    0    09:22:42.767    KryptoMulti9 (EURUSDdu,M1)    2022.02.27 22:00:00   C:\Users\jmjou\AppData\Roaming\MetaQuotes\Tester\D0E8209F77C8CF37AD8BF550E51FF075\Agent-127.0.0.1-3000
PO    0    09:23:02.938    KryptoMulti9 (EURUSDdu,M1)    2022.02.27 22:00:00   Failed to open the file 5002


attached is the first attempt log attached

Files:
Log.JPG  61 kb
 
Check this:
TerminalInfoString(TERMINAL_DATA_PATH)


This returns:
C:\Users\jmjou\AppData\Roaming\MetaQuotes\Tester\D0E8209F77C8CF37AD8BF550E51FF075\Agent-127.0.0.1-3000

Now think where is the file?

It is in [...]\Files\test.csv

So you are trying to open the file at the wrong directory.


 

Error 5002 means ERR_WRONG_FILENAME: https://www.mql5.com/de/docs/constants/errorswarnings/errorcodes.

Try using the option: FILE_COMMON to get rid of the long path name:

FILE_COMMON

4096

The file path in the common folder of all client terminals \Terminal\Common\Files. Flag is used in FileOpen(), FileCopy(), FileMove() and in FileIsExist() functions.

And then look into that folder where you have saved your file with which name.
Dokumentation zu MQL5: Konstanten, Enumerationen und Strukturen / Kodes der Fehler und Warnungen / Ausführungsfehler
Dokumentation zu MQL5: Konstanten, Enumerationen und Strukturen / Kodes der Fehler und Warnungen / Ausführungsfehler
  • www.mql5.com
Ausführungsfehler - Kodes der Fehler und Warnungen - Konstanten, Enumerationen und Strukturen - Nachschlagewerk MQL5 - Nachschlagewerk über die Sprache des algothitmischen/automatischen Handels für MetaTrader 5
 

Thanks a lot Carl

Tried everything possible in mind, but nothing works, except the FILE_COMMON option with no path name before the name of the file ...

Nevertheless, I would be very happy if somebody could tell me exactly how to write the path and name correctly to open a file in a special folder location ...


thanks everybody ....


this works


   string filename="test.csv";
   int hdle=FileOpen(filename,FILE_READ|FILE_COMMON|FILE_CSV|FILE_ANSI,';',CP_UTF8);  
   if(hdle<0){int err=GetLastError();Print("Failed to open the file ",err);}
   else  
   { 
    for (int P=0;P<16;P++){string titre=FileReadString(hdle);} // get rid of the first line
    for (int P=1;P<nbreAlgos+1;P++){
        z[P].magnb=StringToInteger(FileReadString(hdle)); // get the values
     }
    }
   }
 
Volcanbleu #: I would be very happy if somebody could tell me exactly how to write the path and name correctly to open a file in a special folder location ...

Can't be done. Perhaps you should read the manual. FileOpen - File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

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.

Reason: