string reading with sscanf

Discussion in 'C++' started by Bint, Jan 14, 2008.

  1. Bint

    Bint Guest

    i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=barry" etc

    i can use sscanf(string,"success=%d", &d) to get the success value. but
    after that i just want to read name and u pairs until there are no more. if
    Iwere to do a
    sscanf (string,"success=%d&u=%d&name=%s"), that would get me the values of
    the first u/name, right? is there any way to retrieve the string pointer
    position from sscanf so that I can just call it again from that point in the
    string?

    thanks
    B
     
    Bint, Jan 14, 2008
    #1
    1. Advertising

  2. On Mon, 14 Jan 2008 18:06:12 +0100, Bint <> wrote:

    > i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=barry"
    > etc
    >
    > i can use sscanf(string,"success=%d", &d) to get the success value. but
    > after that i just want to read name and u pairs until there are no
    > more. if
    > Iwere to do a
    > sscanf (string,"success=%d&u=%d&name=%s"), that would get me the values
    > of
    > the first u/name, right? is there any way to retrieve the string pointer
    > position from sscanf so that I can just call it again from that point in
    > the
    > string?
    >
    > thanks
    > B
    >
    >

    use ostringstream and istringstream instead of sscanf.
     
    David Côme, Jan 14, 2008
    #2
    1. Advertising

  3. Bint

    Bint Guest

    I don't think my compiler has that. Is that part of standard C/C++? Is
    there an include file for it?

    Thanks
    B


    "David Côme" <> wrote in message
    news:eek:p.t4xkrqjurttu86@debian...
    On Mon, 14 Jan 2008 18:06:12 +0100, Bint <> wrote:

    > i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=barry"
    > etc
    >
    > i can use sscanf(string,"success=%d", &d) to get the success value. but
    > after that i just want to read name and u pairs until there are no more.
    > if
    > Iwere to do a
    > sscanf (string,"success=%d&u=%d&name=%s"), that would get me the values
    > of
    > the first u/name, right? is there any way to retrieve the string pointer
    > position from sscanf so that I can just call it again from that point in
    > the
    > string?
    >
    > thanks
    > B
    >
    >

    use ostringstream and istringstream instead of sscanf.
     
    Bint, Jan 14, 2008
    #3
  4. On Mon, 14 Jan 2008 18:50:20 +0100, Bint <> wrote:

    > I don't think my compiler has that. Is that part of standard C/C++? Is
    > there an include file for it?
    >
    > Thanks
    > B
    >
    >
    > "David Côme" <> wrote in message
    > news:eek:p.t4xkrqjurttu86@debian...
    > On Mon, 14 Jan 2008 18:06:12 +0100, Bint <> wrote:
    >
    >> i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=barry"
    >> etc
    >>
    >> i can use sscanf(string,"success=%d", &d) to get the success value. but
    >> after that i just want to read name and u pairs until there are no
    >> more.
    >> if
    >> Iwere to do a
    >> sscanf (string,"success=%d&u=%d&name=%s"), that would get me the values
    >> of
    >> the first u/name, right? is there any way to retrieve the string
    >> pointer
    >> position from sscanf so that I can just call it again from that point in
    >> the
    >> string?
    >>
    >> thanks
    >> B
    >>
    >>

    > use ostringstream and istringstream instead of sscanf.
    >
    >


    ostringstream and istringstream are standard classes of C++
    PS: Don't reply at top.
     
    David Côme, Jan 14, 2008
    #4
  5. Bint

    Bint Guest

    ok thank you I got it. it's not clear right off how to use that do what I'm
    trying to do though, i haven't really used streams at all
     
    Bint, Jan 14, 2008
    #5
  6. On Mon, 14 Jan 2008 19:00:33 +0100, Bint <> wrote:

    > ok thank you I got it. it's not clear right off how to use that do what
    > I'm
    > trying to do though, i haven't really used streams at all
    >
    >



    Generaly use C++ tools when you can and C tools when you must.
    So use std::string instead of array of char,stream instead of FILE* ....
     
    David Côme, Jan 14, 2008
    #6
  7. Bint

    James Kanze Guest

    On Jan 14, 6:41 pm, David Côme <> wrote:
    > On Mon, 14 Jan 2008 18:06:12 +0100, Bint <> wrote:
    > > i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=barry"
    > > etc


    > > i can use sscanf(string,"success=%d", &d) to get the success
    > > value. but after that i just want to read name and u pairs
    > > until there are no more. if Iwere to do a
    > > sscanf (string,"success=%d&u=%d&name=%s"), that would get me the values
    > > of the first u/name, right? is there any way to retrieve
    > > the string pointer position from sscanf so that I can just
    > > call it again from that point in the string?


    > use ostringstream and istringstream instead of sscanf.


    I'm not sure that either are really appropriate here. If I've
    understood him correctly, he's got a '&' separated list of
    attribute value pairs. The easiest way of handling this is
    probably something like my FieldArray classes; a first pass
    which converts the string into a vector of strings, with one
    attribute value pair in each element. Then split up the
    elements into the attribute and the value (perhaps
    std::transform with a conversion function---boost::regex could
    probably help here).

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jan 14, 2008
    #7
  8. Bint

    Daniel T. Guest

    "Bint" <> wrote:

    > i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=barry" etc
    >
    > i can use sscanf(string,"success=%d", &d) to get the success value. but
    > after that i just want to read name and u pairs until there are no more. if
    > Iwere to do a
    > sscanf (string,"success=%d&u=%d&name=%s"), that would get me the values of
    > the first u/name, right? is there any way to retrieve the string pointer
    > position from sscanf so that I can just call it again from that point in the
    > string?


    I would do something like this:


    #include <algorithm>
    #include <iostream>
    #include <iterator>
    #include <sstream>
    #include <string>

    using namespace std;

    istream& find( istream& is, char c )
    {
    find( istream_iterator<char>( is ), istream_iterator<char>(), c );
    return is;
    }

    int main()
    {
    const char* str =
    "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=barry";

    stringstream ss( str );
    find( ss, '=' );
    int i = 0;
    ss >> i;
    cout << "success = " << i << '\n';
    int u;
    string name;
    while ( find( ss, '=' ) && ss >> u &&
    find( ss, '=' ) && getline( ss, name, '&' ) )
    {
    cout << "u = " << u << " name = " << name << '\n';
    }
    }
     
    Daniel T., Jan 14, 2008
    #8
  9. Bint

    aaragon Guest

    On Jan 14, 3:39 pm, "Daniel T." <> wrote:
    > "Bint" <> wrote:
    > > i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=barry" etc

    >
    > > i can use sscanf(string,"success=%d", &d) to get the success value. but
    > > after that i just want to read name and u pairs until there are no more. if
    > > Iwere to do a
    > > sscanf (string,"success=%d&u=%d&name=%s"), that would get me the values of
    > > the first u/name, right? is there any way to retrieve the string pointer
    > > position from sscanf so that I can just call it again from that point in the
    > > string?

    >
    > I would do something like this:
    >
    > #include <algorithm>
    > #include <iostream>
    > #include <iterator>
    > #include <sstream>
    > #include <string>
    >
    > using namespace std;
    >
    > istream& find( istream& is, char c )
    > {
    > find( istream_iterator<char>( is ), istream_iterator<char>(), c );
    > return is;
    >
    > }
    >
    > int main()
    > {
    > const char* str =
    > "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=barry";
    >
    > stringstream ss( str );
    > find( ss, '=' );
    > int i = 0;
    > ss >> i;
    > cout << "success = " << i << '\n';
    > int u;
    > string name;
    > while ( find( ss, '=' ) && ss >> u &&
    > find( ss, '=' ) && getline( ss, name, '&' ) )
    > {
    > cout << "u = " << u << " name = " << name << '\n';
    > }
    >
    > }


    I would use a tokenizer to do that. You can use the boost tokenizer
    for that giving the token & or you can create your own by reading each
    character and comparing it with '&' to separate the whole string in
    tokens.
     
    aaragon, Jan 14, 2008
    #9
  10. Bint

    Jerry Coffin Guest

    In article <>, says...
    > i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=barry" etc
    >
    > i can use sscanf(string,"success=%d", &d) to get the success value. but
    > after that i just want to read name and u pairs until there are no more. if
    > Iwere to do a
    > sscanf (string,"success=%d&u=%d&name=%s"), that would get me the values of
    > the first u/name, right? is there any way to retrieve the string pointer
    > position from sscanf so that I can just call it again from that point in the
    > string?


    You've gotten a couple of suggestions already, but I'll throw another
    into the mix, just for fun:

    // warning: only minimally tested. Makes no attempt at verifying or
    // reacting reasonably to bad input.
    #include <sstream>
    #include <algorithm>
    #include <map>
    #include <iostream>
    #include <string>
    #include <stdlib.h>

    // the real guts: read a single 'name=value' pair. Written as a template
    // so the value can be an int, string, or anything else we can extract
    // from a stream.
    template<class T>
    std::istream &getvalue(std::istream &is, T &value) {
    // first read the whole 'name=value' pair
    std::string temp;
    std::getline(is, temp, '&');

    // then find the value part:
    int pos = temp.find('=');
    std::string temp2(temp.substr(pos+1,-1));

    // and read the value:
    std::stringstream t(temp2);
    t >> value;
    return is;
    }

    typedef std::pair<int, std::string> uname;

    // not technically allowed, but won't be found unless in std namespace:
    namespace std {
    std::istream &operator>>(std::istream &is, uname &un) {
    getvalue(is, un.first);
    getvalue(is, un.second);
    return is;
    }

    std::eek:stream &operator<<(std::eek:stream &os, uname const &un) {
    return os << un.first << ":\t" << un.second;
    }
    }

    int main() {
    std::stringstream input("success=1&u=0&name=bint"
    "&u=1&name=lucy&u=2&name=barry");

    std::map<int, std::string> values;

    int success;
    getvalue(input, success);

    // read in the data
    std::copy(std::istream_iterator<uname>(input),
    std::istream_iterator<uname>(),
    std::inserter(values, values.begin()));

    // and display what we read:
    std::copy(values.begin(), values.end(),
    std::eek:stream_iterator<uname>(std::cout, "\n"));
    return 0;
    }

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Jan 15, 2008
    #10
    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. Replies:
    3
    Views:
    431
    Dave Thompson
    Jun 8, 2006
  2. Mark
    Replies:
    1
    Views:
    753
    Victor Bazarov
    Aug 19, 2005
  3. Markus Ilmola

    sscanf style string parsing

    Markus Ilmola, Mar 10, 2006, in forum: C++
    Replies:
    6
    Views:
    1,380
    Default User
    Mar 11, 2006
  4. Giff

    sscanf and string

    Giff, Jul 19, 2010, in forum: C++
    Replies:
    5
    Views:
    928
    Juha Nieminen
    Jul 20, 2010
  5. Replies:
    7
    Views:
    488
    Keith Thompson
    Aug 10, 2012
Loading...

Share This Page