read from CSV file ...not read all values

 

hi....am trying to read some values from a CSV file into mt4 using  a simple code 

my  CSV file is  at \MQL4\Files directory

and containe one line of data  

298 ,305 ,132 ,146 ,341 ,376 ,209 ,227


my code only read the first tow value 298 305 and show them on print or comment (either i want to use).

how to make the code show the other values from the Csv file?

one more thing....how could i assign the  print value  or comment value to   

 ObjectCreate

 Function if i want to show any of the values on the screen 

thanks in advance

code

//+------------------------------------------------------------------+
//|                                          read files.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
extern   string   FileName    = "two.csv";



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
int start()

{
{
  int handle=-1,count,line_count=1;
  
  string data[1][8];
   

  handle=FileOpen(FileName,FILE_CSV|FILE_ANSI|FILE_READ,",",CP_ACP);//

 if ( handle<0) 
 {  
 
 Print("File "+FileName+" not found.");
 }
 else
 
  Print("File "+FileName+" successfully open.");
  while (FileIsEnding(handle)==false)
  {
  
  data[count][0]=FileReadString(handle,8);
  data[count][1]=FileReadString(handle,8);
 Print("Line number"+IntegerToString(line_count)+".data 1= "+data[count][0]);//the printed is 298
 Print("Line number"+IntegerToString(line_count)+".data 1= "+data[count][1]);//the printed is 305


  if (FileIsEnding(handle) == true)line_count++;
  count++;
  }

/// how could i asign the printed or comment integer value to Objectcreate?

     ObjectDelete("Diff");
     ObjectCreate("Diff", OBJ_LABEL, 0, 0, 0);
      ObjectSetText("Diff",DoubleToStr(15,5),10, "Arial", LawnGreen);
      ObjectSet("Diff", OBJPROP_XDISTANCE,20);
      ObjectSet("Diff", OBJPROP_YDISTANCE, 60);
      ObjectSet("Diff", OBJPROP_CORNER, 1);
    ObjectCreate("Diff", OBJ_LABEL, 0, 0, 0);
    
     FileClose(handle);
    
 }


 
  return(0);
}
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
Predefined Macro Substitutions - Named Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
William Roeder:
  1. Read the next two.
  2. Perhaps you should read the manual. StringFormat
       How To Ask Questions The Smart Way. 2004
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

  William Roeder....i will say ....lets wait may be ...some one will give a solution to the code

 
any note could be useful ....
 
abd-:
any note could be useful ....

your code is bit of a mess, you probably need to nail some of the basics before starting with files etc.

that said I have modified the file part of your code so that it works, not how I would write it myself but this way you understand your starting point.

Look carefully at the changes,

some further pointers:

You do not need a multi-dimensional array to store the items in, you could just use two pointers or pointer and pointer+1 but however you decide you should only read 1 item from the file in each loop otherwise you may run into the end of the file, (unless you are going to do a further file check inside the loop before reading a second item)

You don't need to specify the length of the item to be read in a CSV file the separator defines that

You need to maintain the size of the destination array to make sure it does not overflow.

If you are only reading in integers/numbers then you could use a more efficient file format.

extern   string   FileName    = "two.csv";

void start()
{
   int handle=-1;
   int count = 0;  
   string data[99];
   
   handle=FileOpen(FileName,FILE_CSV|FILE_ANSI|FILE_READ,",",CP_ACP);
   
   if ( handle<0) 
   {  
      Print("File "+FileName+" not found.");
   }
   else
   Print("File "+FileName+" successfully open.");
   
   while(!FileIsEnding(handle)) 
   {
      data[count] = FileReadString(handle);
      Print(count + " = " + data[count]);
      count++;
   }
    
   FileClose(handle);
   return;  
}
Documentation on MQL5: Common Functions / GetTickCount64
Documentation on MQL5: Common Functions / GetTickCount64
  • www.mql5.com
GetTickCount64 - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Paul Anscombe:

your code is bit of a mess, you probably need to nail some of the basics before starting with files etc.

that said I have modified the file part of your code so that it works, not how I would write it myself but this way you understand your starting point.

Look carefully at the changes,

some further pointers:

You do not need a multi-dimensional array to store the items in, you could just use two pointers or pointer and pointer+1 but however you decide you should only read 1 item from the file in each loop otherwise you may run into the end of the file, (unless you are going to do a further file check inside the loop before reading a second item)

You don't need to specify the length of the item to be read in a CSV file the separator defines that

You need to maintain the size of the destination array to make sure it does not overflow.

If you are only reading in integers/numbers then you could use a more efficient file format.

Hi it.s me abd- ....am using smart phone now and forget my old password and  the orignal registration info(even my email pass)   ...am at hospital now but i dicide to make a new user  and  MUST reply to   Paul Anscombe  and say THANKS  very much....recently i cannot test the code using smart phone but later i will do....thanks very much and moderator sorry for using a new user ...
 
Abd-1:
Hi it.s me abd- ....am using smart phone now and forget my old password and  the orignal registration info(even my email pass)   ...am at hospital now but i dicide to make a new user  and  MUST reply to   Paul Anscombe  and say THANKS  very much....recently i cannot test the code using smart phone but later i will do....thanks very much and modarater sorry for using a new user ...

your welcome 

 

FileReadString from csv reads only to the first delimiter:  https://www.mql5.com/en/docs/files/filereadstring

You should have 8  data[count][x] lines with x from 0 to 7.

Documentation on MQL5: File Functions / FileReadString
Documentation on MQL5: File Functions / FileReadString
  • www.mql5.com
FileReadString - File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Gyula Szajlai:

FileReadString from csv reads only to the first delimiter:  https://www.mql5.com/en/docs/files/filereadstring

You should have 8  data[count][x] lines with x from 0 to 7.

that's not necessary  the code I provided works perfectly fine and read string reads the next string from the current file pointer.

 
Gyula Szajlai:

FileReadString from csv reads only to the first delimiter:  https://www.mql5.com/en/docs/files/filereadstring

You should have 8  data[count][x] lines with x from 0 to 7.

Thanks very much Gyula Szajlai....i will take this in considaration when dealing with Csv in future....     recently i cannot test the code or modify it using smart phone but later i will do   .    thanks