Read CSV file

 

Hello,

Sorry there are a million topics on this already but I've read through heaps of them and can't get it to work.

I directly copied the example from the documentation, but changed from a bin to a csv. 

//--- parameters for data reading
string InpFileName="hi.csv"; // file name
string InpDirectoryName="Data"; // directory name

//--- open the file
ResetLastError();
int file_handle=FileOpen("Data"+"//"+InpFileName,FILE_CSV|FILE_READ);

if(file_handle!=INVALID_HANDLE)
  {
   PrintFormat("%s file is available for reading",InpFileName);
   PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
   //--- close the file
   FileClose(file_handle);
   PrintFormat("Data is read, %s file is closed",InpFileName);
  }
else
   PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());



I place my file in the directory, and every time I run the expert in tester, I get the error code 5004 and the file gets deleted.

OOP in MQL5 by Example: Processing Warning and Error Codes
OOP in MQL5 by Example: Processing Warning and Error Codes
  • 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.
 
J Sky :


The file is in the 'Files' directory - IT'S IMPORTANT !!!


Here is the code:

//+------------------------------------------------------------------+
//|                                                 FileOpen csv.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property version   "1.000"
#property description "Open the CSV file at the specified path"
#property script_show_inputs
//--- input parameters
input string   InpDirectoryName  = "Data";   // Directory name
input string   InpFileName       = "hi.csv"; // File name
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   int filehandle=FileOpen(InpDirectoryName+"\\"+InpFileName,FILE_READ|FILE_CSV);
   if(filehandle!=INVALID_HANDLE)
     {
      PrintFormat("%s file is available for reading",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
      //--- close the file
      FileClose(filehandle);
      PrintFormat("Data is read, %s file is closed",InpFileName);
     }
   else
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
  }
//+------------------------------------------------------------------+

Here's a good result:

2021.08.18 07:21:24.911 FileOpen csv (AUDUSD,M10)       hi.csv file is available for reading
2021.08.18 07:21:24.911 FileOpen csv (AUDUSD,M10)       File path: C:\Users\barab\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\Files\
2021.08.18 07:21:24.911 FileOpen csv (AUDUSD,M10)       Data is read, hi.csv file is closed
Files:
Reason: