Is there any way to find out the length of a txt file ( number of lines)?
I need to be able to find this out in order to poplate an array with an unspecified amount of data from a txt file.
:eek:
Printable View
Is there any way to find out the length of a txt file ( number of lines)?
I need to be able to find this out in order to poplate an array with an unspecified amount of data from a txt file.
:eek:
You can find out its byte size, but not the number of lines without reading it all in. This is because text files are just binary files interpreted differently, where each line ends with a newline character '\n'.
So, you can read it through and count the lines, or just read each line into a buffer that should be long enough, say 2048 bytes, then use a dynamic array of some description.
stat() or _stat() is available in all c implementations.
Code:#include <sys/stat.h>
int fileLen(char *path){
//in VC++ use the _stat() function
struct _stat buffer;
int i = _stat( path, &buffer );
return buffer.st_size;
}