Is there a simple code or function I can implement into my code to read the number of lines in a given file?
thanx
jujubeats
Is there a simple code or function I can implement into my code to read the number of lines in a given file?
thanx
jujubeats
Use a counter and the getline function.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
i was on the correct path using the getline but i havent been able so far let me keep on trying
thanx
Don't know of any standard C function that does this, but you could just scan the file and count the number of newline characters occurring.
>but you could just scan the file and count the number of newline characters occurring.
This screams fencepost error. What if the last line is terminated with end-of-file instead than a newline? Your count would be off by one.
My best code is written with the delete key.
the file only contains binary inputs
so scanf would be also a good resource?
oh no scanf is the same as cin
>the file only contains binary inputs
When you want to count the lines in a file, it's usually expected that the file contains text. This is mostly because newlines vary depending on the system, and any attempt to read one in a binary oriented file could easily fail. Maybe you should give us an example of the file, describe what you mean by a "line", and explain what you intend to do with the count. Then we'll be in a better position to offer suggestions beyond the usual suspects.
My best code is written with the delete key.
well sticking to words instead of binary inputs
lets say I have
dogs
cats
plane
fish
i already opened the file I just need to put a subroutine to count
the lines in that text file i opened i.e. 4 lines in this previous example
and then display the number of lines along with its contents on another file
(which i've done with ofstream but im missing the actual code to read the # of lines succesfully )