I have a list of numbers which looks like this: 1.234D+1
or 1.234D-02
. I want to read the file using C. The function atof
will merely
You can take advantage of strtod and strtok:
#include
#include
#include
int main(void) {
char in[] = "1.234D+1 ABCD 1.234D-02 5\n";
char *cursor;
char *endp;
for ( cursor = strtok(in, " "); cursor; cursor = strtok(NULL, " ")) {
double d = strtod(cursor, &endp);
if ( *endp == 'D' ) {
*endp = 'e';
d = strtod(cursor, &endp);
}
if ( endp != cursor ) {
printf("%e\n", d);
}
}
return 0;
}
Output:
E:\> cnv 1.234000e+001 1.234000e-002 5.000000e+000