Does someone know a C++ funktion that returns the number of lines of a .txt File?
Does someone know a C++ funktion that returns the number of lines of a .txt File?
Nope don't know of any funktions. Though it wouldn't be hard to create a function to do it.
Do excuse Thantos, I think he forgets that you are from austria and spelling function they way its spelled there.
I prefer the C way of reading files so I'll give a C example.
Of course the above is simplistic and doesn't handle lines above 4k, but its a start.Code:#include <cstdio> #include <iostream> // This function return the number of lines or -1 if the file doesn't exist long lineCount(const char *filename) { long count = -1; FILE *file; char buffer[4096]; if((file = std::fopen(filename, "r"))) { do { count++; } while(std::fgets(buffer, 4096, file)); std::fclose(file); } return count; } int main(int argc, char **argv) { if(argc == 2) { std::cout << argv[1] << " contains " << lineCount(argv[1]) << " lines" << std::endl; } return 0; }
Is there any way of discovering the size of the file? I know that I can read until EOF, in partial buffers, I am just curious about such a function.
Thanks any help.
Nothing more to tell about me...
Happy day =)
http://cboard.cprogramming.com/searc...searchid=77170Originally Posted by gustavosserra
Its been answered before, IIRC its not that difficult
C++ is C++ regardless of any written or spoken language the user may use.Do excuse Thantos, I think he forgets that you are from austria and spelling function they way its spelled there.
actually, my c++ teacher is from sudan or someplace lie that and she says there is c++ for different (human) languages
pfft!....i would need the 124 crayon set for that....i only have the 36
Oh... Crayons?! Where!
You'll be able to find system specific ways to get the size of the file without opening it and reading the whole thing, but to find the number of lines, you will need to open it (as you need to actually see what is inside).
C/C++ itself (the actual code portion) will be the same universally. I'm speaking more in terms of how the word function is spelled may vary from language to language (spoken language, that is).