reading files

how can i know number of lines in an input text file during run time so i can make a loop for the same number of lines in the ifstream?
You don't need to know the number of lines because you can loop using istream::getline or std::getline:

1
2
3
4
5
6
ifstream input_file("foobar.txt");
std::string buffer;
while(std::getline(input_file,buffer))
{
  // do something with contents of buffer
}
what does std::string buffer; mean?
It means "create an object of type string, which is part of the std namespace, and call this new object buffer".

If creating an object is something you are not familiar with, you are not ready to start using input text files. You should start with something like int x; and see how you go with that.



Topic archived. No new replies allowed.