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 )
So is it binary or text? That's kind of an issue in determining a solution. If it's text, you can just loop with getline:
Code:while ( getline ( in, line ) ) { // Increment a count, store the line, whatever }
My best code is written with the delete key.
its just a series of matrices
instead of cat and dog
its more like
000
010
100
111
but i just need to do it for a file at a time
such as this example
>its more like
Okay, that's text which just happens to represent binary values. So you can approach the problem from that perspective and use getline.
My best code is written with the delete key.
thanx
im trying to but i think im using it as to read from the keyboard instead of my file thats why its not working
At this point you need to post your code, or we'll be completely unable to help you (or refuse to).
My best code is written with the delete key.
I apologize heres what my code looks like
the andop(and operator) only contains a four line matrixCode:#include "stdafx.h" #include <fstream> #include <iostream> using namespace std; int main() { char str[20]; fstream file_op("c:\\andop.txt",ios::in); while(file_op >> str) cout << " \n" << str << endl; file_op.close(); ofstream file_wr("c:\\outfile.txt", ios::out); file_wr.close(); return 0; }
Last edited by jujubeats; 11-02-2006 at 05:08 PM.