how many rows in a file?

 

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.

 

untested:

int getCsvLines( const string fName, string &l[], int flag=0, ushort sepLine='\n') { // alternative to FileOpen 
   uchar Bytes[]; 
  return( FileLoad(fName, Bytes, flag) ? StringSplit(CharArrayToString(Bytes), sepLine, l) : 0);
}
 
KjLNi: I have tried "file size", but it gives the size in terms of bites, not rows.

You can't use that because your rows length is variable (text file).

Just read in the lines until end of file, counting as you go.

Why do you need the count?

 
Carl Schreiber #:

untested:

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!!


 
William Roeder #:

You can't use that because your rows length is variable (text file).

Just read in the lines until end of file, counting as you go.

Why do you need the count?

Hello and thanks,


why I need the count?

because later I will write content of the file to an array.

and I would like to make the size of the array dynamic - depending on the number of lines in the file.

 
William Roeder #:

You can't use that because your rows length is variable (text file).

Just read in the lines until end of file, counting as you go.

Why do you need the count?

pls can you give me a tip how to do this (read the lines until the end, count as you go ...)

 
KjLNi #: pls can you give me a tip how to do this (read the lines until the end, count as you go ...)

When in doubt, think!

struct FileStructure{
   int i; double d; string s;
   read(int h){
      i=FileReadInteger(h);
      d=FileReadDouble(h);
      s=FileReadString(h);
   }
};
⋮
   int h=FileOpen(…); if(h ==  INVALID_HANDLE) …
   FileStructure line;
   int count=0;
   while( !FileIsEnding(h) ){ line.read(h); ++count; }
   PrintFormat("File has %d lines", count);;
 
William Roeder #:

When in doubt, think!

hello, and thanks.

I have tried this, but I get an error about the "read".

 
KjLNi #: I get an error about the “read”.

I gave you “a tip how to do this,” not working code. You should have already known how to do that.

The error is obvious. If you can't see it, stop trying to code until you learn the language.

 
William Roeder #:

I gave you “a tip how to do this,” not working code. You should have already known how to do that.

The error is obvious. If you can't see it, stop trying to code until you learn the language.

Hello, I can see your point. Sorry, I am a week-end worrier. I will do as you said and continue to learn the language.

 
Carl Schreiber #:

untested:

I like this style of code.