I have to process line by line. Each line has a answer, which is stored in the vector result.
Here is my algorithm:
1 2 3 4 5 6
std::ifstream infile("RPN.txt");
std::vector<int> result;
int answer;
// evaluate the answer of line 1 ...
result.push_back(answer);
// repeat for the next line until end of file (no next line)
I attempted to use infile >> in order to extract the data in the text file. But fail to stop when the end of a line is reached.
What is the correct approach I should use? (without storing the whole line by getline and then processing the string)
If you need to evaluate everything, one part at a time, do what you were doing, but then add: if (infile.peek() == '\n' || infile.peek() == '\r')
That should check to see if the next character is an end of line character, but it's not as surefire, and requires a little more effort. My suggestion is to use getline to store the string and parse it as if your file was only one line long. Then grab the next line.
I don't know what would be best for your specific situation, but there are tons of different options out there, and I still believe getline is your best option.