Another Failed to open the CSV file:5002 ERROR

 
Hi All!

I have read the documentation and read all the related topics in the forum, but still cant find out what the problem is.

This is the path of the file:

C:\Users\danie\AppData\Roaming\MetaQuotes\Terminal\2C68BEE3A904BDCEE3EEF5A5A77EC162\MQL4\Files\test.csv
(This is the real path and also this is printed at Print(filename);

This is the content of the csv: (I even tryed it with an empty file.)
2021.02.01,00:00,1.21301,1.21364,1.20562,1.20594,63743,1.2130572,1.1665786,1.1952606,-0.0075899999999999,-0.007117,0.039361,0.00802,1.198349999999996

And this the code snippet where it fails:
void OnStart()
{

    Print("START");
    string csvFilePath = "test.csv";

    string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
    string filename=terminal_data_path+"\\MQL4\\Files\\"+csvFilePath;

    Print(filename);
    
    int fileHandle = FileOpen(csvFilePath,FILE_READ|FILE_CSV,",");
    
    if (fileHandle == INVALID_HANDLE)
    {
        Print("Failed to open the CSV file:", GetLastError());
        return;
    }
...

The encoding of the file is UTF-8.

How can I troubleshoot, why it cant open the file?

Thank you
 

5002

ERR_FILE_WRONG_FILENAME

Wrong file name

 
Alain Verleyen #:

5002

ERR_FILE_WRONG_FILENAME

Wrong file name

Yes, I have looked this up and found it in the documentation. 
The file name is not wrong.
That is why I decised to post.
 
Kiloromeo #: The file name is not wrong.
    string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
    string filename=terminal_data_path+"\\MQL4\\Files\\"+csvFilePath;

Yes, it is. You can not have a colon in a file name.

File Write Problem (Error: 5002) - Expert Advisors and Automated Trading - MQL5 programming forum #1-2 (2020)

 
Kiloromeo #:
Yes, I have looked this up and found it in the documentation. 
The file name is not wrong.
That is why I decised to post.

Please provide a csv file.

Also

Please don't post randomly in any section. Your question is not related to the section you posted.

MQL4 and MetaTrader 4, has it's own section on the forum.

I will move your topic to the correct section later, please don't create another topic.

 

You should not append the full path in front of the filename.

Read the FileOpen documentation and check the example on that page.

Pay attention to the comments:


//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- incorrect file opening method
   string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
   string filename=terminal_data_path+"\\MQL4\\Files\\"+"fractals.csv";
   int filehandle=FileOpen(filename,FILE_WRITE|FILE_CSV);
   if(filehandle<0)
     {
      Print("Failed to open the file by the absolute path ");
      Print("Error code ",GetLastError());
     }
//--- correct way of working in the "file sandbox"
   ResetLastError();
   filehandle=FileOpen("fractals.csv",FILE_WRITE|FILE_CSV);
   if(filehandle!=INVALID_HANDLE)
     {
      FileWrite(filehandle,TimeCurrent(),Symbol(), EnumToString(ENUM_TIMEFRAMES(_Period)));
      FileClose(filehandle);
      Print("FileOpen OK");
     }
   else Print("Operation FileOpen failed, error ",GetLastError());
//--- another example with the creation of an enclosed directory in MQL4\Files\
   string subfolder="Research";
   filehandle=FileOpen(subfolder+"\\fractals.txt",FILE_WRITE|FILE_CSV);
      if(filehandle!=INVALID_HANDLE)
     {
      FileWrite(filehandle,TimeCurrent(),Symbol(), EnumToString(ENUM_TIMEFRAMES(_Period)));
      FileClose(filehandle);
      Print("The file most be created in the folder "+terminal_data_path+"\\"+subfolder);
     }
   else Print("File open failed, error ",GetLastError());
  }