sscanf ?

This is a discussion on sscanf ? within the C++ Programming forums, part of the General Programming Boards category; Dear All I am trying to use sscanf() function to read a string and assign two double which are separeted ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    23

    Question sscanf ?

    Dear All

    I am trying to use sscanf() function to read a string and assign two double which are separeted by comma to two different variables. Code part is in the following.

    Code:
    long double T,V;
    string tmp1="2.4E-09,-0.007000001";
     sscanf(tmp1.c_str(),"%13.15f,%13.15f",&T,&V);
    cout<<T<<" "<<V<<"\n";
    it gives :
    2.51292e+3273 -0.6614e-2303

    What is the problem?

    I would be pleaed if you could help.

    thank you
    Last edited by CornedBee; 05-19-2008 at 05:58 AM. Reason: Corrected the bad slash.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You seem to have incorrect format flags for sscanf. It doesn't seem to accept the width specifications and "f" is for float. What worked correctly for me (replacing long double with long) is
    Code:
    int main()
    {
        double T,V;
        string tmp1="2.4E-09,-0.007000001";
        sscanf(tmp1.c_str(),"&#37;lf,%lf",&T,&V);
        cout<< setprecision(13) << T<<" "<<V<<"\n";
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Banned
    Join Date
    Nov 2007
    Posts
    678
    You can also try this:
    Code:
    stringstream ss;
    string tmp1("2.4E-09,-0.007000001");
    ss.str(tmp1);
    double a, b;
    char comma;
    ss >> a >> comma >> b;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf and string handling question
    By ursula in forum C Programming
    Replies: 14
    Last Post: 05-30-2009, 02:21 AM
  2. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 05:42 AM
  3. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  4. sscanf question
    By Zarkhalar in forum C++ Programming
    Replies: 6
    Last Post: 08-03-2004, 07:52 PM
  5. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21