c - strtod is returning 0 -
i've been working on project deals input file looks following:
a 0.0345 b 0.3945 ... z 0.2055 basically, i'm reading each line, after reading line, want pull only double out of string. i'm trying utilize strtod() , everyone's examples seem utilize fine in example, returns 0.
here code:
for (count = 0; count < 26; count++) { char buffer[100]; /* buffer line read fgets */ char *letters; /* stores string calculated strtod */ double relfreq = 0.0; if (fgets(buffer, 100, stream) != null) { relfreq = (double)strtod(buffer, &letters); /* since format 0.0000, we're grabbing double */ *(sampleone + count) = relfreq; /* add together relfreq array */ } } through utilize of debugger, buffer has right data, instance a 0.0345\n when strtod() returns value it's 0. help appreciated!
since first character non-numeric, strtod should returning letters equal buffer (the first character invalid), indicating nil has been read.
you should able prepare passing &buffer[2], number starts:
relfreq = strtod(&buffer[2], &letters); demo.
c string double
No comments:
Post a Comment