how many rows in a file? - page 2

 
KjLNi:

hello there, I would like to check, how many rows are in a file.

I am working with CSV files.

I have tried "file size", but it gives the size in terms of bites, not rows.

If you are aware of any trick, thanks in advance.

This is for MQL5.

just for fun here is my take on it, but attention, I am not a pro.

// help-program to count the rows in the file
   
   string ResultRead;
   
   int cnt=0; // use the variable to count the number of file lines
   while(!FileIsEnding(fHandle)){
         ResultRead = FileReadString(fHandle);
         if(FileIsLineEnding(fHandle)) {
            cnt++; }
            }
            
    //Comment("check");

   // end of help-program to count the rows in the file

when this is finished, 

the number of rows will be in the variable cnt.

 
KjLNi #:

just for fun here is my take on it, but attention, I am not a pro.

when this is finished, 

the number of rows will be in the variable cnt.

Which you don’t need anyway just read into a dynamic array and resize as you go until end of file  no need to read the file twice as you plan to
 
KjLNi #:

hello and thanks.

My interpretation of this:

the function "getCsvLines" can get the number of lines.

When I call the function, I believe I have to provide a number of parameters.

The first one is fName, ok I have this.

But what about the others?

   string&[],int,ushort

Please let me know what I should provide for those.

Thanks!!


This function should return the whole file as a string array of lines so and the number of lines for the loop:

  1. &l[] is the array that is returned and
  2. ushort sepLine='\n' is the separator to split the array.

PS. Thanks fxsaber :)


 
fxsaber #:

I like this style of code.

Not a surprise
 
Paul Anscombe #:
Which you don’t need anyway just read into a dynamic array and resize as you go until end of file  no need to read the file twice as you plan to

oho, cool, may I ask how you work with a "dynamic array"?

 
KjLNi #: oho, cool, may I ask how you work with a "dynamic array"?

You need to read the documentation and learn the language.

T array[]; int count=0
loop{
   ArrayResize(array, count+1); array[count++]=value;
}