How this function work? O.o

hi, I have this function, when I refer to it. cout << search() it works just fine. I kinda copied it off of someone code, modify it till it work and put in my code, but I don't have a good understanding of it. It returns to me the number of line in a text file. But I don't know why it work. Why is the string "numberOfLine" in there? I don't see it as serving any purpose. It be great if someone can give me a good explaination of the it. Thank you...:) I am very nooby, only started for a week. :(


1
2
3
4
5
6
7
8
9
10
int search() 
{
	ifstream myfile("DataLog.txt");
	string numberOfLine;
	int count(0);
while(getline(myfile, numberOfLine)){
                         count++;
                         }
return(count);
}
Last edited on
ifstream myfile("DataLog.txt") opens the file and 'attaches' it to myfile

It then takes a line at a time from the file, incrementing a counter as it goes until it gets to the end of the file. Finally it returns the value of the counter.

getline(myfile, numberOfLine)
puts the next line of text from the file pointed to by myfile, into string numberOfLine.
count is incremented and it goes back and gets the next line until getline() fails because there are no more lines of text in the file.

The string numberOfLine is needed to make getline() work even though it is never used.
Topic archived. No new replies allowed.