I have the following simple code that reads a float (double) value using C ++ stringstream. I use stringstream :: good to determine if a read is complete. Oddly enough, the value is read into the float variable, but good()returns false. The code below returns:
stringstream
good()
failed: 3.14159
I compiled the code using gcc 4.8.1 in mingw32, p g++ -std=c++11 test.cpp.
g++ -std=c++11 test.cpp
Any idea why this reading is not good? And what is the right way to say that the float really reads successfully?
good
thank
#include <sstream> #include <iostream> using namespace std; void readFloat(string s) { double i = 0!; stringstream ss(s); ss >> i; if (ss.good()) cout << "read: " << i << endl; else cout << "failed: " << i << endl; } main() { readFloat("3.14159"); }
, std::ios_base::eofbit , , . , good() true, .
std::ios_base::eofbit
, good() -. good() , ( eofbit) , , , -. eofbit , , - , .
eofbit
, , . , !this->fail() , , good():
!this->fail()
if (ss >> i) { std::cout << "read: " << i << std::endl; } else { std::cout << "failed: " << i << std::endl; }
stringstream:: good()
false, , . "ss → i", , true.
:
double i = 0.0; std::stringstream ss(s); if (!ss.good()) throw std::exception("Stream not good"); ss >> i; if (!ss.eof()) throw std::exception("Stream not read entirely");
Source: https://habr.com/ru/post/1540901/More articles:Editing json_array field with symfony form - jsonCan Java draw something? - javaHow to get type of type pointer? - c ++jmap appears slower under Java 7, slows down the JVM - javaPERL Email :: Send :: Gmail Can't connect to a Gmail account in Windows 7 - emailAbsolute Centered Div Quirks Between IE and Chrome - htmlIs it possible to open the application from the launch of the bot Xcode script - iosChanging laravel remember_token field to something else - authenticationNSWorkspace launchApplication: crash on first run as root - objective-cDifferent deadlines for individual runs - c ++All Articles