views:

182

answers:

5

How do you read in a double from a file in C++?

For ints I know you can use the getline() and then atoi, but I am not finding an array to double function. What is available for reading in doubles, or converting a char array to a double?

+4  A: 

You can use stream extraction:

std::ifstream ifs(...);
double d;
ifs >> d;

This work provided that other then whitespace, the next data in the stream should be a double in textual representation.

After the extraction, you can check the state of the stream to see if there were errors:

ifs >> d;
if (!ifs)
{
    // the double extraction failed
}
R Samuel Klatchko
This is wrong. Test the stream for failure (`.fail()`, which is subtly different than `.good()`, which is equivalent to `.rdstate() == 0`). Use `if (!(ifs >> d)) {/*failed*/}` or `ifs >> d; if (!ifs) {/*..*/}` or (the generally more useful) `if (ifs >> d) {/*use d*/} else {/*failed*/}`.
Roger Pate
(Using `!stream` is the same as `stream.fail()`, using `bool(stream)` is the same as `!stream.fail()`.)
Roger Pate
@RogerPate - thank you.
R Samuel Klatchko
A: 

You can use atof. It works just as atoi, but returns double.

x13n
This doesn't actually address the question.
EvilTeach
it isn't proper c++ either.
wilhelmtell
@wilhelmtell: It's much more effective to stick to the technical merits, because it is perfectly standard C++. "Proper" depends on *many* subjective and situational factors.
Roger Pate
@Roger by improper i mean it's nasty. you practically need to do some parsing of the string yourself if you want to check `atof()` worked correctly. The question asked about reading a double from a file in C++, and this just isn't the right answer. it isn't a good advice even if the question would be about extracting a string from a stream.
wilhelmtell
@wilhelmtell: It's not good advice for this question, but that doesn't make atof "improper C++" at all.
Roger Pate
+2  A: 

Do not consider using atof(), or any of the ato.. functions, as they do not allow you to diagnose errors. Take a look at strtod and strtol. Or use the stream extraction operators.

anon
Reason for downvote?
anon
+1 because I was unaware of strtod and strtol
JonM
-1 for use of phrase "Reason for downvote?"
sblom
@sblom Is it the grammar you object to? If so, please suggest improvements.
anon
wilhelmtell
A: 

You can leverage istringstream For example, here are toDouble and toInt:

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;
}
Mohamed Mansour
This already exists in boost, in a much better form including error checking, as lexical_cast.
Roger Pate
@Roger Pate: Yes, it does. This is useful though for cases where you can't use boost.
Billy ONeal
@Billy: It is no more useful than atof, which Neil correctly [points out](http://stackoverflow.com/questions/2615078/how-to-read-in-a-double-from-a-file-in-c/2615119#2615119) should be avoided (unless you've already validated the input, but that's not true here).
Roger Pate
This doesn't answer the question.
wilhelmtell
(@Billy: Since you got a mysterious downvote on your now-deleted answer, I feel compelled to say that wasn't me, nor have I downvoted any answer here for merely suggesting atof or omitting error handling. I've been out of votes for the past 18 hours and have my next 30 already bookmarked for 0:01 GMT too. :P)
Roger Pate
@Roger Pate: No problem. Reason I deleted it is because I considered your comment and I agree ;)
Billy ONeal
+1  A: 

I'm wondering, does one need to be careful about locale settings (e.g. a locale could use comma instead of dot to separate the decimal part) or do stringstreams always default to some standard "C locale" notation?

bohan