Parsing char array to CString

Discussion in 'C++' started by John Smith, Sep 6, 2003.

  1. John Smith

    John Smith Guest

    Hi all,

    I have a char buff[100] which contains "1234 ABCD 5678".

    I can't remember what function I used before to parse the string into
    individual variables. It was something like this:

    forgotenFunction(var1, var2, var3, buff, "%d %s %d")

    It gave me this:

    var1 = "1234"
    var2 = "ABCD"
    var3 = "5678"

    Any ideas? I will appreciate it.

    J.
     
    John Smith, Sep 6, 2003
    #1
    1. Advertising

  2. John Smith

    John Smith Guest

    Ok, I found it. It is sscan().


    "John Smith" <> wrote in message
    news:bjdfeu$26p4$...
    > Hi all,
    >
    > I have a char buff[100] which contains "1234 ABCD 5678".
    >
    > I can't remember what function I used before to parse the string into
    > individual variables. It was something like this:
    >
    > forgotenFunction(var1, var2, var3, buff, "%d %s %d")
    >
    > It gave me this:
    >
    > var1 = "1234"
    > var2 = "ABCD"
    > var3 = "5678"
    >
    > Any ideas? I will appreciate it.
    >
    > J.
    >
    >
     
    John Smith, Sep 6, 2003
    #2
    1. Advertising

  3. John Smith

    phykell Guest

    int sprintf(char *buffer, const char *format [,argument] ... );

    "John Smith" <> wrote in message
    news:bjdfeu$26p4$...
    > Hi all,
    >
    > I have a char buff[100] which contains "1234 ABCD 5678".
    >
    > I can't remember what function I used before to parse the string into
    > individual variables. It was something like this:
    >
    > forgotenFunction(var1, var2, var3, buff, "%d %s %d")
    >
    > It gave me this:
    >
    > var1 = "1234"
    > var2 = "ABCD"
    > var3 = "5678"
    >
    > Any ideas? I will appreciate it.
    >
    > J.
    >
    >
     
    phykell, Sep 8, 2003
    #3
  4. phykell wrote:

    > int sprintf(char *buffer, const char *format [,argument] ... );


    Please don't top-post. Re-read section 5 of the FAQ for posting
    guidelines. You might also be interested in section 3 of RFC 1855
    (Netiquette Guide):

    http://www.parashift.com/c -faq-lite/
    http://www.dtcc.edu/cs/rfc1855.html

    Your answer is also wrong, and wouldn't be a very good suggestion even
    if it wasn't. sprintf is a very dangerous function that is very
    difficult to use correctly.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Sep 8, 2003
    #4
  5. "John Smith" <> wrote in message
    news:bjdfeu$26p4$...
    > Hi all,
    >
    > I have a char buff[100] which contains "1234 ABCD 5678".
    >
    > I can't remember what function I used before to parse the string into
    > individual variables. It was something like this:
    >
    > forgotenFunction(var1, var2, var3, buff, "%d %s %d")
    >
    > It gave me this:
    >
    > var1 = "1234"
    > var2 = "ABCD"
    > var3 = "5678"
    >
    > Any ideas? I will appreciate it.
    >
    > J.
    >
    >


    If you'd like to use C++ rather than C, you'd use something like;

    #include <iostream>
    #include <string>
    #include <sstream>
    int main(int argc, char* argv[])
    {
    std::string str("1234 ABCD 5678");
    std::stringstream sstr(str);
    int var1,var3;
    std::string var2;
    sstr >> var1 >> var2 >> var3;
    std::cout<<var1<<"\n"<<var2<<"\n"<<var3<<"\n";

    return 0;
    }

    It is a lot safer. However one advantage in sscanf is that it handles fixed
    width numerics much more tidily. The stringstream operators have the notion
    of delimiters hardwired in (which very strong coupling to the data I find
    pretty weird in an OO library). The setwidth() manipulators only work if the
    next extraction is to a string.

    If you are doing lots of this stuff it is worth looking at the boost
    libraries for <lexical_cast>
    The old gcc stream class had stream::sscanf(). I think BSD still has this
    implementation though I stand to be corrected.


    Pat
     
    Pat McCormack, Sep 9, 2003
    #5
    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. Tim  Wong
    Replies:
    5
    Views:
    22,016
    Tim Wong
    Jan 21, 2005
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,187
    Ian Collins
    May 9, 2006
  3. Replies:
    3
    Views:
    772
  4. davidb
    Replies:
    0
    Views:
    832
    davidb
    Sep 1, 2006
  5. davidb
    Replies:
    6
    Views:
    1,605
    Default User
    Sep 1, 2006
Loading...

Share This Page