How to read in double from a file in C ++
4 answers
You can use stream extraction:
std::ifstream ifs(...);
double d;
ifs >> d;
This work stipulates that other spaces, the following data in the stream must be double in the textual representation.
Once retrieved, you can check the status of the stream to see if there were any errors:
ifs >> d;
if (!ifs)
{
// the double extraction failed
}
+3
source to share
You can use istringstream For example, there are double and toInt here:
double toDouble(string s) {
double r = 0;
istringstream ss(s);
ss >> r;
return r;
}
int toInt(string s) {
int r=0;
istringstream ss(s);
ss >> r;
return r;
}
0
source to share