Number parsing ideas (from FORTRAN)

I need to read in a lot (~225k) numbers formatted the following way: 1.2340000000000000D+03. I'd like to read in the number, and re-format it into either scientific notation or general notation (i.e., 123.4). All have the same number of significant figures (I believe this is the FORTRAN double precision convention).

Any ideas of how to do this efficiently? Is there a standard C++ library component that can handle FORTRAN-friendly numbers? I can't read it in just as a double or float because I lose the "D+03" information at the end.

Thanks in advance.
Did you try replacing 'D' with 'E' and then reading them as doubles?
As MiiNiPaa pointed out:

1.2340000000000000D+03   // Fortran
1.2340000000000000E+03   // C++


http://www.fortran.com/F77_std/rjcnf0001-sh-4.html#sh-4.5
http://www.cplusplus.com/reference/ios/scientific/
Last edited on
closed account (j3Rz8vqX)
If you are reading the FORTRAN numbers, from a file, as string then you can:

Use string.find() to find 'D' and replace it with 'E'.

After that, a possible solution would be to use atof() with the constant_string and assigning it to a type of double.

I do not want to provide solutions without attempts, first, so I will only post what the results might look like:
Fortran Doubles:
1.2340000000000000D+00
1.2340000000000000D+01
1.2340000000000000D+02
1.2340000000000000D+03
1.2340000000000000D+04
1.2340000000000000D+05
1.2340000000000000D+06
1.2340000000000000D+07
1.2340000000000000D+08
1.2340000000000000D+09

C++ Strings:
1.2340000000000000E+00
1.2340000000000000E+01
1.2340000000000000E+02
1.2340000000000000E+03
1.2340000000000000E+04
1.2340000000000000E+05
1.2340000000000000E+06
1.2340000000000000E+07
1.2340000000000000E+08
1.2340000000000000E+09

C++ Doubles:
1.234
12.34
123.4
1234
12340
123400
1.234e+006
1.234e+007
1.234e+008
1.234e+009

possible solution would be to use atof()
Or stod()/stof()
Use string.find() to find 'D' and replace it with 'E'.
Or use std::replace
If you are reading the FORTRAN numbers, from a file, as string then you can:
Replace D with E in file itself prior to reading and then just read them as doubles.

I do not mean to say that your solutions are bad, I merely providing another ways to do that (what to actually should be done depends on many conditions which are unknown to us at this time)
closed account (j3Rz8vqX)
Yes, there are many ways of doing this.

Below are the libraries:

atof() will require the <cstdlib> (Must be included)
.find() will require the <string> (Native, Not necessary with most modern compilers)
stod()/stod() requires <string> (Native, plus requires c++11)
replace() will require <algorithm> (Must be included to use)

Question:
Replace D with E in file itself prior to reading and then just read them as doubles
Can you extend on the method to do so; I am carious, as to what you are introducing. Normally i'd just read the data in, then modify the data. I'm thinking, you've got an automated subroutine, in mind. But brute force is definitely an option; especially with editors widely available.
Last edited on
Wow thanks for the responses everyone.

@Dput and MiiNiPaa:
So what I ended up doing was the following:

fscanf(inFile,"%s",&MyString);
c = strchr(MyString,'D');
*c = 'E';
sscanf(MyString,"%e",&junkd);

I read the FORTRAN number in as a string, searched it for D and replaced it with E, and then used the sscanf function to store it as a float type.
You could just use atof() instead of sscanf(). Way safer and faster.
Topic archived. No new replies allowed.