How can I know the end of line of a text file?

I have a text file "RPN.txt".
2 3 4 + *
7 4 3 + - 2 +
12 46 + 23 -
1 5 + 8 4 1 - - *
6 3 - 5 * 9 +
12 23 11 + 45 * +
2 11 + 4 7 + *
231 111 +

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)
Last edited on
1
2
3
4
#include <string>
// ...
// That should get you the entire line as a string
std::getline(infile, answer);

getline() will read infile until it reaches the first carriage return/new line character and store it in answer.
I want to process in the text file, but not to store the whole line first.
How can I do this without std::getline?
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.
After detecting the next character is an end of line character, how to jump into the next line?
All of the functions you need, can be found here: http://www.cplusplus.com/reference/fstream/fstream/

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.
What is the reason that C++ does not offer the convenient function eoln()?
What is the reason that C++ does not offer the convenient function eoln()?


C++ offers the convenient function getline, and the convenient stringstream family of types to turn that line into a stream.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <fstream>
#include <sstream>
#include <stack>
#include <string>
#include <cassert>
#include <cctype>

int evaluate(int lhs, int rhs, const std::string& op)
{
    bool valid_operator_match = true;
    int result;

    if (op == "+")
        result = lhs + rhs;
    else if (op == "-")
        result = lhs - rhs;
    else if (op == "*")
        result = lhs * rhs;
    else if (op == "/")
        result = lhs / rhs;
    else
        valid_operator_match = false;

    assert(valid_operator_match);

    std::cout << '\t' << lhs << ' ' << op << ' ' << rhs << " = " << result << '\n';
    return result;
}

bool is_operator(const std::string& token)
{
    return token.size() == 1 && !std::isdigit(token[0]);
}


int main()
{
    std::stack<int> values;
    std::ifstream infile("RPN.txt");

    std::string line;
    while (std::getline(infile, line))
    {
        assert(values.empty());

        std::istringstream stream(line); 
        std::cout << "Evaluating \"" << line << "\"\n";

        while (stream)
        {
            std::string token;

            while (stream >> token && !is_operator(token))
                values.push(std::stoi(token));

            if (stream)
            {
                assert(values.size() > 1);
                int rhs = values.top();
                values.pop();

                int lhs = values.top();
                values.pop();

                values.push(evaluate(lhs, rhs, token));
            }
        }

        assert(values.size() == 1);
        std::cout << "Result is " << values.top() << "\n\n" ;
        values.pop();
    }
}
Last edited on
Thanks all!
Finally I have used the getline.
Topic archived. No new replies allowed.