Reading from TXT file

 

Dear

 I need to create a structure named "table" to hold the data from the txt file (attached). How can I do that? I tried this script below but did not work.  

//+------------------------------------------------------------------+
//|                                                      Stategy.mq5 |
//|                                                    Victor Carozo |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Victor Carozo"
#property link      "https://www.mql5.com"

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
  struct Table
  {   
      double line; 
      datetime date;
      string symbol;
      double price;

  };
   
   Table table[];
   
   int i=0;
   
   int fileHandle = FileOpen("Sample.txt",FILE_READ|FILE_TXT);
   
   if(fileHandle!=INVALID_HANDLE) 
    {
     while(FileIsEnding(fileHandle) == false)
       {
   
  
        ArrayResize(table,ArraySize(table) +1 );     
        table[i].line = FileReadNumber(fileHandle);
        table[i].date = FileReadDatetime(fileHandle);
        table[i].symbol = FileReadString(fileHandle);
        table[i].price = FileReadNumber(fileHandle);
  
     
       i++;
      
      }
     FileClose(fileHandle);   
       
    }
   for ( i=0; i< ArraySize(table); i++) Print(table[i].price);
   Print(fileHandle);  
   

}
//+------------------------------------------------------------------+
Files:
Sample.txt  1 kb
 
Victor Carozo:

Dear

 I need to create a structure named "table" to hold the data from the txt file (attached). How can I do that? I tried this script below but did not work.  

You need to open the file in CSV mode like so:

short delimiter=',';
int fileHandle = FileOpen("Sample.txt",FILE_READ|FILE_CSV,delimiter);

And table line should be an int:

  struct Table
  {   
      int line; 
      datetime date;
      string symbol;
      double price;

  };
...
  table[i].line = (int)FileReadNumber(fileHandle);
 
lippmaje:

You need to open the file in CSV mode like so:

And table line should be an int:

Dear lippmaje


Thanks but didn't work. 

 
Victor Carozo:

Thanks but didn't work. 

Then what exactly does not work for you?

Note that you don't make a difference whether the file is empty or just not found. Handle your errors, they are telling you something.

 
lippmaje:

Then what exactly does not work for you?

Note that you don't make a difference whether the file is empty or just not found. Handle your errors, they are telling you something.

Thanks again lipmaje. Let me try to explain. When I compile the updated scrip I got no errors however, When I run attached to a chart I got 0 in the first line and 1 at the second line, means that my table is not holding the data, right?

 
Victor Carozo:

Thanks again lipmaje. Let me try to explain. When I compile the updated scrip I got no errors however, When I run attached to a chart I got 0 in the first line and 1 at the second line, means that my table is not holding the data, right?

You need to show some screenshot or log. From what I got I cannot tell you more. The file reading and table construction is correct - you may check for errors at other places. What is sample.txt, how is it created, is it in the right location etc. And please do not say "I got a 0 and then I got a 1." This is meaningless.
 
Victor Carozo:

Dear

 I need to create a structure named "table" to hold the data from the txt file (attached). How can I do that? I tried this script below but did not work.  


Hi Victor,

You need to indicate how the file is formatted in your FileOpen command. Which means you'd need to change your line 

int fileHandle = FileOpen("Sample.txt",FILE_READ|FILE_TXT);

into 

int fileHandle = FileOpen("Sample.txt",FILE_READ|FILE_CSV,',');

so that the File reading functions know how to handle it.



 

Dear

Here the code updated as suggested. 


//+------------------------------------------------------------------+
//|                                                      Stategy.mq5 |
//|                                                    Victor Carozo |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Victor Carozo"
#property link      "https://www.mql5.com"

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
  struct Table
  {   
      int line; 
      datetime date;
      string symbol;
      double price;
     
  };
   
   Table table[];
   
   int i=0;
   
   short delimiter=';';
   int fileHandle = FileOpen("Sample.txt",FILE_READ|FILE_CSV,delimiter);
   
   if(fileHandle!=INVALID_HANDLE) 
    {
     while(FileIsEnding(fileHandle) == false)
       {
   
  
        ArrayResize(table,ArraySize(table) +1 );     
        table[i].line = (int) FileReadNumber(fileHandle);
        table[i].date = FileReadDatetime(fileHandle);
        table[i].symbol = FileReadString(fileHandle);
        table[i].price = FileReadNumber(fileHandle);
        
     
       i++;
      
      }
     
     FileClose(fileHandle);   
       
    }
    for ( i=0; i< ArraySize(table); i++) Print(table[i].date); 
  
   
  
   

}
//+------------------------------------------------------------------+

I attached the .txt file for conference and compile results. I still not hold the data from ''sample.txt'' in the ''trade'' structure, anyone has another suggestion?

Files:
Sample.txt  1 kb
chart.gif  14 kb
compile.gif  15 kb
 

What do you see when you add this line:

   short delimiter=';';
   int fileHandle = FileOpen("Sample.txt",FILE_READ|FILE_CSV,delimiter);
   if(fileHandle==INVALID_HANDLE) Alert("could not open file, error: "+(string)GetLastError()); 
 
lippmaje:

What do you see when you add this line:

Same as before...

1970.01.01 00:00:00
 

and when you add this to the bottom:

Print("table size: ",ArraySize(table));
for(i=0; i<ArraySize(table); i++) Print(table[i].line," ",table[i].date," ",table[i].symbol," ",table[i].price);