0

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)

Ðаn
  • 10,934
  • 11
  • 59
  • 95
Da Mike
  • 447
  • 4
  • 17
  • 7
    After `>> s1 >> s2`, your stringstream is in bad state - `eof()` returns true (any future read operation must fail, and s1 and s2 are not modified printing data entered in first iteration). So move stringstream into while body scope, or call `clear()` to reset flags before reading new data at every iteration of loop. – rafix07 Mar 13 '20 at 14:18
  • @rafix07 Thank you, it worked! But why is it in bad sate after the `>> s1 >> s2` expression? – Da Mike Mar 13 '20 at 14:33
  • https://wandbox.org/permlink/OqcWV8ln7Z07JGo4 – Marek R Mar 13 '20 at 15:00

1 Answers1

0

As mentioned in comment put

    sline.clear();  

in while loop,this resets I/O Status.Because after this sline >> s1 >> s2; the eof bit is set to true.

see this:

    while ( getline(inputFile, line) )
    {
            sline.clear();

            sline.str(line);

            sline >> s1 >> s2;

            cout << "Just read: " << s1 << " " << s2 << endl;
    }

You can also simply the code as follow:

 #include <iostream>
 #include <fstream>
 #include <string>
 #include <stdlib.h>

 int main() 
 {

         std::ifstream inputFile("inputs.txt");

         if(inputFile.is_open()==0)
         {
             std::cout<<"File Error";
             exit(1);
         }

         std::string line;

         while ( getline(inputFile, line) )
                    std::cout << "Just read: " <<line << std::endl;

         inputFile.close();
         return 0;
 }

EDIT:

if you want separate two strings try this

    std::string s1,s2;

    while ( inputFile>>s1>>s2 )
               std::cout << "Just read: " <<s1<<" "<<s2 << std::endl; 
srilakshmikanthanp
  • 2,231
  • 1
  • 8
  • 25
  • Thanks for your answer! The thing is I want to separate each line in two variables s1 and s2. So the last piece of code is not what I need. – Da Mike Mar 13 '20 at 15:41
  • @Da Mike If you want to separate into two variables you can also try, i mentioned it in the EDIT – srilakshmikanthanp Mar 13 '20 at 15:47