reading files

May 18, 2012 at 5:22pm
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?
May 18, 2012 at 5:26pm
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
}
May 18, 2012 at 5:49pm
what does std::string buffer; mean?
May 18, 2012 at 6:01pm
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.