CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2001
    Posts
    24

    string conversion

    Hi gang

    I have a file that contains floating point numbers in text like this
    -0.321435435D-02
    0.4324325435D-02
    1.4343243434D+00
    2.4343434343D+5
    -0.432432443D+2
    etc
    etc

    i need to be able to loop through the file and convert each line ot a double where d-xx is the . placement
    for example
    0.4324325435D-02 = 0.004324325435
    and
    -0.432432443D+2 = -043.2432443

    any tips on achieving this.
    Thanks in advance
    Scott

  2. #2
    Join Date
    May 2001
    Posts
    472
    Originally posted by MSDN:

    double atof( const char *string );

    The string argument to atof has the following form:
    [whitespace] [sign] [digits] [.digits] [ {d | D | e | E }[sign]digits]
    That should help you.
    Ce n'est que pour vous dire ce que je vous dis.

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    standard C++ i/o should take care of this.
    (following code not tested ...)

    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	ifstream in("input.txt");
    
    	if (!in)
    	{
    		cout << "could not open file" << endl;
    		return 1;
    	}
    
    	double d;
    	while (in >> d && !in.fail())
    	{
    		cout << "read from file : " << d << endl;
    	}
    
    	return 0;
    }

  4. #4
    Join Date
    May 2001
    Posts
    24
    You missed the point. I can read each line of text from the file but i need to convert the string format of a double (non standard) to a double.
    thanks
    Scott

  5. #5
    Join Date
    May 2001
    Posts
    472
    The atof function does just that. The format of your string corresponds to the format that the function accepts, so using it should be an easy task. Check MSDN for more information on atof.
    Ce n'est que pour vous dire ce que je vous dis.

  6. #6
    Join Date
    Jul 2002
    Posts
    4

    maybe you needed

    ...

    char ss[256];

    while(...)
    {

    //read double d from your file.

    sprintf(ss,"%f",d);
    cout<<<ss<<endl;

    //or use cout.width(..) and cout.setf(..)

    ...
    }

  7. #7
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    Are you just mixing up external and internal representations of the same number? If you read 0.4324325435D-02 and printf it in f format, you will get 0.004324325435. If you printf it in E format, you will get 0.4324325435E-02. Those are just external fomats. Internally, the number is the same.
    Succinct is verbose for terse

  8. #8
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    I think you're confused where I was confused.

    0.4324325435D-02 is a legal format for a double value.

    Jeff

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by scottincz
    You missed the point. I can read each line of text from the file but i need to convert the string format of a double (non standard) to a double.
    thanks
    Scott
    There is another standard function besides atof() and that is strtod(). Either one should work for you.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured