Reading D19.12 format and converting to double

Hello guys,

I have a text file with the following type of data:

"5.800000000000D+01 2.387500000000D+01 4.843415890576D-09 1.127192966832D+00"

How I can read the this type of data from the file and convert it to double or any numeric format in order to use them in some calculations?

The numbers represented there are :
58 23.8750 4.843415890576000e-09 1.127192966832

PS: I am a beginner in the C++. My only background is in Matlab Environment.

Any help would be really welcomed.

Thank you.
Here is the general idea, will leave you to figure out how to deal with the file work as it will be good for you to learn. fstream will handle all that.

http://www.cplusplus.com/reference/fstream/fstream/
http://www.cplusplus.com/reference/string/string/find/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <string>

int main() 
{
	//Loop through the file and put each number into a string, easy with >>  lots of tutorials online
	
	std::string S= "5.80000D+1";
	
	std::size_t Found = S.find("D");  // Could do error checking here to make sure it exists etc
	double Number = stod(S.substr(0, Found));    // stod converts string to a double
	double Extension = stod(S.substr(Found + 1));   // Skip the D, will be +1 in this case

	if(Extension < 0)
	{
		for(int i = 0; i > Extension; i--)
		     Number /= 10;    // Move the decimal point right
	}
	
	else
	{
		for(int i = 0; i < Extension; i++)
		     Number *= 10; // Move the decimal point left
	}
	
	std::cout << Number << std::endl;   // 58
	
	return 0;
}
Thank you! :)
Topic archived. No new replies allowed.