Re: Problems converting a substring to double

Discussion in 'C++' started by Ivan Vecerina, Jul 18, 2003.

  1. "Daniel Wetzler" <> wrote in message
    news:bf8mo8$bsgr5$...
    ....
    | I import a string-variable calles "zeile" into my routine.
    | This Variable hods a line extracted from a file which loos like this :
    |
    | ATOM 1 N LEU A 1 46.582 41.671 38.773 1.00 25.95
    | 1OVO 117
    |
    | I would like to extract the float values liek e.g. 46.852 as float
    | or double.
    ....
    | string temp;
    ....
    | temp = zeile.substr(31,2);
    | xkoor = atof(temp);
    | // this doesn't work
    The following would do:
    xkoor = atof( temp.c_str() );

    std::atof is a C function which doesn't know about std::string.

    But you may want to consider using an std::stringstream to
    read space-delimited tokens from the line of text.
    It makes things much easier:

    #include <sstream>

    int protein::lese_pdb_atom (string zeile)
    {
    std::istringstream src(zeile);
    string bezeichnung;
    int i1, i2;
    char c1, c2;
    string s1;
    float f;
    src >> bezeichnung >> i1 >> c1 >> s1 >> c2 >> i2 >> f;
    // etc...
    }

    Given the text line above, this would extract "ATOM" into
    bezeichnung, etc, and 46.582 into f.



    hth,
    --
    Ivan Vecerina, Dr. med. <> http://www.post1.com/~ivec
    Brainbench MVP for C++ <> http://www.brainbench.com
    Ivan Vecerina, Jul 18, 2003
    #1
    1. Advertising

  2. Hallo,

    thanks for the advice.
    I tried it that way before but the line belongs to a special standard
    with says that a special column of the file holds special informations.
    But sometimes not all informations are held in each line of the file.
    So the method with the stream doesn't work for every file.
    I have to check for the positions in each line.


    Greetings,

    Daniel



    Ivan Vecerina wrote:
    > "Daniel Wetzler" <> wrote in message
    > news:bf8mo8$bsgr5$...
    > ...
    > | I import a string-variable calles "zeile" into my routine.
    > | This Variable hods a line extracted from a file which loos like this :
    > |
    > | ATOM 1 N LEU A 1 46.582 41.671 38.773 1.00 25.95
    > | 1OVO 117
    > |
    > | I would like to extract the float values liek e.g. 46.852 as float
    > | or double.
    > ...
    > | string temp;
    > ...
    > | temp = zeile.substr(31,2);
    > | xkoor = atof(temp);
    > | // this doesn't work
    > The following would do:
    > xkoor = atof( temp.c_str() );
    >
    > std::atof is a C function which doesn't know about std::string.
    >
    > But you may want to consider using an std::stringstream to
    > read space-delimited tokens from the line of text.
    > It makes things much easier:
    >
    > #include <sstream>
    >
    > int protein::lese_pdb_atom (string zeile)
    > {
    > std::istringstream src(zeile);
    > string bezeichnung;
    > int i1, i2;
    > char c1, c2;
    > string s1;
    > float f;
    > src >> bezeichnung >> i1 >> c1 >> s1 >> c2 >> i2 >> f;
    > // etc...
    > }
    >
    > Given the text line above, this would extract "ATOM" into
    > bezeichnung, etc, and 46.582 into f.
    >
    >
    >
    > hth,
    Daniel Wetzler, Jul 18, 2003
    #2
    1. Advertising

  3. "Daniel Wetzler" <> wrote in message
    news:bf8p34$c9l2d$...
    | thanks for the advice.
    | I tried it that way before but the line belongs to a special standard
    | with says that a special column of the file holds special informations.
    | But sometimes not all informations are held in each line of the file.
    | So the method with the stream doesn't work for every file.
    | I have to check for the positions in each line.
    Ok.
    In this case, here's a suggestion for a general approach to the
    problem:

    // Convert substr of 'line' and store in 'outputVal'
    // Returns true if extraction was ok and no other data was left.
    template<typename T>
    bool extract( std::string const& line
    , unsigned offset, unsigned len
    , T& outputVal )
    {
    std::istringstream src( line.substr(offset,len) );
    return ( src>>outputVal ) // extract the value
    && src.eof(); // check there's no data left
    }

    Which could be used as follows with multiple variable types:
    ok = extract(zeile,18,3,bezeichnung);
    // how you handle ok depends on your format spec...

    ok = extract(zeile,31,2,xkoor);

    This approach may or may not fit your spec,
    but it could be a convenient shortcut...


    Cheers,
    --
    Ivan Vecerina, Dr. med. <> http://www.post1.com/~ivec
    Brainbench MVP for C++ <> http://www.brainbench.com
    Ivan Vecerina, Jul 18, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris \( Val \)
    Replies:
    1
    Views:
    421
    Daniel Wetzler
    Jul 18, 2003
  2. Daniel Wetzler
    Replies:
    0
    Views:
    360
    Daniel Wetzler
    Jul 18, 2003
  3. Sydex
    Replies:
    12
    Views:
    6,430
    Victor Bazarov
    Feb 17, 2005
  4. sparkydarky

    Converting a substring to Integer

    sparkydarky, Feb 5, 2008, in forum: C++
    Replies:
    26
    Views:
    802
    James Kanze
    Feb 9, 2008
  5. Replies:
    3
    Views:
    182
    Sherm Pendley
    Aug 3, 2005
Loading...

Share This Page