Reading file with different types of data issue.

 

Hello fellow traders,

I'm trying to read a txt file with 3 collumns containg different type of data [datetime, double, double]. I get no error but I can't have my algo to read the file following the FileReadArray instructions. fileHandle is invalid.

Any idea?

Cheers!


//---fileTest.txt data

2022.04.26\t1.2354\t1.8524


int fileHandle;

struct prices{
   datetime    date;
   double      bid;
   double      ask;   
};


int OnInit(void){

   Print("OnInit");
   
   prices arr[];
   
   bool ifCond = open_file();
   int size = 0;
   
   if(ifCond){
      //--- read all data from the file to the array
      FileReadArray(fileHandle, arr);
      //--- receive the array size
      size = ArraySize(arr);      
   }
   
   close_file();
   
   //--- Show if fileHandle is valid (true) and the size of the arr (=1)
   Comment(ifCond, "  ", size);
   
   return(INIT_SUCCEEDED);
}


bool open_file(){

   string fileName = path+"//Terminal//Common//testFile.txt";
   fileHandle = FileOpen(fileName, FILE_READ | FILE_TXT);
   if(fileHandle != INVALID_HANDLE)
      return true;
   else
      return false;
}


void close_file(){

   FileClose(fileHandle);
}
 

Put the file 'fileTest.txt' in the shared folder:


Here is the code that reads the file:

//+------------------------------------------------------------------+
//|                                      Reading different types.mq5 |
//|                              Copyright © 2022, Vladimir Karputov |
//|                      https://www.mql5.com/en/users/barabashkakvn |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2022, Vladimir Karputov"
#property link      "https://www.mql5.com/en/users/barabashkakvn"
#property version   "1.00"
#property script_show_inputs
//--- input parameters
input string   InpFileName  = "fileTest.txt";
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- correct way of working in the "file sandbox"
   ResetLastError();
   int file_handle=FileOpen(InpFileName,FILE_READ|FILE_TXT|FILE_COMMON);
   if(file_handle!=INVALID_HANDLE)
     {
      PrintFormat("%s file is available for reading",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_COMMONDATA_PATH));
      //--- additional variables
      string str;
      //--- read data from the file
      while(!FileIsEnding(file_handle))
        {
         //--- read the string
         str=FileReadString(file_handle,-1);
         //--- print the string
         PrintFormat(str);
        }
      //--- 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());
  }
//+------------------------------------------------------------------+
 

Thanks for the reply, Vladimir Karputov,

The algo runs without any issues, however, when printing the string, it gives me these symbols. I changed the data in the file to just "line-1\nline-2\n...line-5) but I still get the same symbols (see screenshot_1).


I'm using a UTF-8 txt file (see screenshot_3) .


Thanks

Files:
 
Vladimir Karputov #:

Put the file 'fileTest.txt' in the shared folder:


Here is the code that reads the file:

Change this to

   int file_handle=FileOpen(InpFileName,FILE_READ|FILE_TXT|FILE_ANSI,0,CP_UTF8);
 
amrali #:

Change this to

I get an error 5004. OpenFile function fails to open the file.

 
totavios #:

I get an error 5004. OpenFile function fails to open the file.

Put the file in MQL5\Files folder via the menu File -> Open Data Folder


#property copyright "Copyright © 2022, Vladimir Karputov"
#property link      "https://www.mql5.com/en/users/barabashkakvn"
#property version   "1.00"
#property script_show_inputs
//--- input parameters
input string   InpFileName  = "fileTest.txt";
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- correct way of working in the "file sandbox"
   ResetLastError();
   int file_handle=FileOpen(InpFileName,FILE_READ|FILE_TXT|FILE_ANSI,0,CP_UTF8);   
   if(file_handle!=INVALID_HANDLE)
     {
      PrintFormat("%s file is available for reading",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
      //--- additional variables
      string str;
      //--- read data from the file
      while(!FileIsEnding(file_handle))
        {
         //--- read the string
         str=FileReadString(file_handle,-1);
         //--- print the string
         PrintFormat(str);
        }
      //--- 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());
  }
//+------------------------------------------------------------------+
  
 
totavios #:

Thanks for the reply, Vladimir Karputov,

The algo runs without any issues, however, when printing the string, it gives me these symbols. I changed the data in the file to just "line-1\nline-2\n...line-5) but I still get the same symbols (see screenshot_1).


I'm using a UTF-8 txt file (see screenshot_3) .


Thanks

Version 1.001

//+------------------------------------------------------------------+
//|                                      Reading different types.mq5 |
//|                              Copyright © 2022, Vladimir Karputov |
//|                      https://www.mql5.com/en/users/barabashkakvn |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2022, Vladimir Karputov"
#property link      "https://www.mql5.com/en/users/barabashkakvn"
#property version   "1.001"
#property script_show_inputs
//--- input parameters
input string   InpFileName    = "fileTest.txt";
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- correct way of working in the "file sandbox"
   ResetLastError();
   int file_handle=FileOpen(InpFileName,FILE_READ|FILE_TXT|FILE_ANSI|FILE_COMMON,0,CP_ACP);
   if(file_handle!=INVALID_HANDLE)
     {
      PrintFormat("%s file is available for reading",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_COMMONDATA_PATH));
      //--- additional variables
      string str;
      //--- read data from the file
      while(!FileIsEnding(file_handle))
        {
         //--- read the string
         str=FileReadString(file_handle,-1);
         //--- print the string
         PrintFormat(str);
        }
      //--- 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());
  }
//+------------------------------------------------------------------+
 
Vladimir Karputov #:

Version 1.001

Worked beautifully! Thanks for taking the time to help me, Vladimir. 
Files: