Q:

How to look ahead multiple steps while reading files in C++

I have an input file that consists of multiple integers separated by white spaces or newline characters.
EX:
7\n
2 2\n
3 4\n
\n
8\n
3 1\n
5 6\n
7 7\n
\n
3\n
1 2\n
3 4\n

.... and so on.
I need to decide how many different input sets I have based on this input file. I am unable to do this. Any tips?

A:

Read the file line by line. After each read, check what the next character in the stream is. If it's '\n' (or EOF), you're at the end of input set.
std::string line;
std::ifstream stream;
size_t sets = 0;

while (std::getline(stream, line)) {

    // do something with line

    int next = stream.get()
    if (next == '\n' || next == EOF)
        ++sets;
    else
        stream.putback(next);
}

