So there is the inputs.txt
file looking like this
john connor
neil young
cat stevens
and after seeing this question, I'm trying to read it like this
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main() {
ifstream inputFile("inputs.txt");
string s1, s2;
string line;
stringstream sline;
while ( getline(inputFile, line) ) {
sline.str(line);
sline >> s1 >> s2;
cout << "Just read: " << s1 << " " << s2 << endl;
}
inputFile.close();
return 0;
}
but the output I get is
Just read: john connor
Just read: john connor
Just read: john connor
Why is this happening? What am I getting wrong? Any help would be appreciated!
Edit: After fixing my problem using rafix07's comment, I noticed that if I add a few empty lines in the inputs.txt
file the output I get is
john connor
neil young
cat stevens
the output I get is
Just read: john connor
Just read: neil young
Just read: neil young
Just read: cat stevens
Just read: cat stevens
Just read: cat stevens
(which is not wanted behavior)