I can't understand why the second >> fails. Am I doing something wrong or missing some code?
float f1, f2;
ss >> f1;
getline(file, str);//when packed inside if(), evalueates to true
ss >> f2; //when packed inside if(), evalueates to false - but why it fails?
std::cout<<"str = "<<str<<"\n";
std::cout<<"ss.str() = "<<ss.str()<<"\n";
std::cout<<"f1 = "<<f1<<"\nf2 = "<<f2<<"\n";
str = 0.120000
ss.str() = 0.120000
f1 = 0.12
f2 = 2.06831e+032
file >> f1;
file >> f2;
You have to add the following statement before the second attempt to read:
Because when you've read the first line, the string stream contains
ss>>f1 will cause
ss to reach the end of file. So that the eof flag is set.
When you reset the stringstreams's content with
str(), you don't reset the state flags, so that the attempt to read will fail. adding the ss.clear() after you've reset the stringstreams's content will correct this situation.