I have a simple program where I am trying to redirect a file to stdin and print out each line. I know each line has two numbers but I don't know how many lines there are.
#include <bits/stdc++.h>
using namespace std;
int main()
{
ifstream in("input.txt");
cin.rdbuf(in.rdbuf()); // redirect to stdin
while(!cin.eof())
{
int a, b;
std::cin >> a >> b;
std::cout << "a = " << a << ", b = " << b << std::endl;
}
return 0;
}
If I run this on "input.txt"
1 2
3 4
I get the output
a = 1, b = 2
a = 3, b = 4
a = 3, b = 4
Why is the second line being read twice by cin?