input from a file

I have a .txt file with 5 lines of text. Each line is a different quote that a character in the main function will be saying. I am using an ifstream and a getline in the main function for each time he says something.

My problem is that when I get to the end of the file, I need to go back to the beginning to get the same lines again. How do I do this?
You have two choices. One, you could use one of the member functions of ifstream to move it back to the beginning, or you could close and re-open the same file.

However, you could also just store the text into a vector of strings, and read that so you only have to read the file once. What exactly are you going to do with the data?
I'm just going to cout the data to the user. It really has no effect on the rest of the program.
Storing them in a vector<string> is the most efficient route. You can then just do something like this:

cout << quote[count++ % quote.size()] << endl;

That just prints the next quote, automagically looping through the list over and over.
Cool! Thanks!
Topic archived. No new replies allowed.