sscanf feature in C++?

Discussion in 'C++' started by Matt, Oct 18, 2004.

  1. Matt

    Matt Guest

    Do we have sscanf feature in C++? I guess since C++ is superset of C.
    So the following are valid C++ code. Is that true?

    char* buf = "10:25:33";
    sscanf(buf, "%d:%d:%d", &h, &m, &s);

    Please advise. thanks!!
    Matt, Oct 18, 2004
    #1
    1. Advertising

  2. Matt

    Howard Guest

    "Matt" <> wrote in message
    news:...
    > Do we have sscanf feature in C++? I guess since C++ is superset of C.
    > So the following are valid C++ code. Is that true?
    >
    > char* buf = "10:25:33";
    > sscanf(buf, "%d:%d:%d", &h, &m, &s);
    >
    > Please advise. thanks!!


    I don't see it, either in my headers or in my books. (Also, C++ is *not* a
    superset of C, if I recall.)

    Look into the stringstream class. I think that's what you want.

    -Howard
    Howard, Oct 18, 2004
    #2
    1. Advertising

  3. "Matt" <> wrote in message
    news:...
    > Do we have sscanf feature in C++? I guess since C++ is superset of C.
    > So the following are valid C++ code. Is that true?
    >
    > char* buf = "10:25:33";
    > sscanf(buf, "%d:%d:%d", &h, &m, &s);
    >
    > Please advise. thanks!!


    C++ incorporates the C standard library, so the above is perfectly valid
    C++.

    john
    John Harrison, Oct 18, 2004
    #3
  4. Matt

    Mike Wahler Guest

    "Howard" <> wrote in message
    news:gzWcd.8966$...
    >
    > "Matt" <> wrote in message
    > news:...
    > > Do we have sscanf feature in C++? I guess since C++ is superset of C.
    > > So the following are valid C++ code. Is that true?
    > >
    > > char* buf = "10:25:33";
    > > sscanf(buf, "%d:%d:%d", &h, &m, &s);
    > >
    > > Please advise. thanks!!

    >
    > I don't see it, either in my headers or in my books.


    You should find declarations of all the C(90) library functions
    in your headers, or your C++ implementation is not complete.

    I would not be surprised if it's not in your books, though.
    Most (imo correctly) focus upon the C++ library.

    > (Also, C++ is *not* a
    > superset of C, if I recall.)


    This is an issue of often lengthy debate. It's 'sort of'
    a superset, but not in the strict sense. But if you have
    a compliant C++ implementation, you can depend upon the
    existence of all the C library functions from C90.

    > Look into the stringstream class. I think that's what you want.


    It does have its advantages. But 'scanf()' et. al. do have theirs
    as well.

    -Mike
    Mike Wahler, Oct 18, 2004
    #4
  5. Matt

    Mike Wahler Guest

    "Matt" <> wrote in message
    news:...
    > Do we have sscanf feature in C++?


    Yes. But you might want to investigate the C++ IOStreams
    equivalent: stringstreams.

    > I guess since C++ is superset of C.


    Not strictly.

    > So the following are valid C++ code. Is that true?
    >
    > char* buf = "10:25:33";


    Should be:

    const char *buf = "10:25:33";

    or:

    char buf[] = "10:25:33";

    > sscanf(buf, "%d:%d:%d", &h, &m, &s);


    The syntax is correct, yes. But it needs the 'framework',
    e.g. #include <cstdio> (or <stdio.h>), definitions of
    'h', 'm', 's', and a properly defined 'main()' function.

    -Mike
    Mike Wahler, Oct 18, 2004
    #5
  6. Matt

    Howard Guest

    "Mike Wahler" <> wrote in message
    news:VeXcd.621$%...
    > "Howard" <> wrote in message
    > news:gzWcd.8966$...
    >>
    >> "Matt" <> wrote in message
    >> news:...
    >> > Do we have sscanf feature in C++? I guess since C++ is superset of C.
    >> > So the following are valid C++ code. Is that true?
    >> >
    >> > char* buf = "10:25:33";
    >> > sscanf(buf, "%d:%d:%d", &h, &m, &s);
    >> >
    >> > Please advise. thanks!!

    >>
    >> I don't see it, either in my headers or in my books.

    >
    > You should find declarations of all the C(90) library functions
    > in your headers, or your C++ implementation is not complete.
    >


    Ah, you are correct. I didn't have the path to those included in my search
    paths, I guess. When I searched the whole source tree, I found it in
    cstdio.

    -Howard
    Howard, Oct 18, 2004
    #6
  7. Matt

    Default User Guest

    John Harrison wrote:

    >
    > "Matt" <> wrote in message
    > news:...
    > > Do we have sscanf feature in C++? I guess since C++ is superset of
    > > C. So the following are valid C++ code. Is that true?
    > >
    > > char* buf = "10:25:33";
    > > sscanf(buf, "%d:%d:%d", &h, &m, &s);
    > >
    > > Please advise. thanks!!

    >
    > C++ incorporates the C standard library, so the above is perfectly
    > valid C++.



    With the correct header and (possibly) namespace resolution, of course.
    He either needs to include <stdio.> (deprecated) or <cstdio>. In the
    latter case, sscanf() will be in the std namespace and will need to be
    explicitly scoped or require a using declaration.



    Brian
    Default User, Oct 18, 2004
    #7
  8. Matt wrote:
    > Do we have sscanf feature in C++? I guess since C++ is superset of C.


    C++ supports the standard C functions for historical reasons.
    That is intended for developers who want to migrate their old C code to C++.


    > So the following are valid C++ code. Is that true?
    >
    > char* buf = "10:25:33";
    > sscanf(buf, "%d:%d:%d", &h, &m, &s);
    >
    > Please advise. thanks!!


    Check out istringstream. That looks better compared to sscanf.

    --
    Karthik. http://akktech.blogspot.com .
    ' Remove _nospamplz from my email to mail me. '
    Karthik Kumar, Oct 19, 2004
    #8
  9. Matt

    Default User Guest

    Default User wrote:

    > He either needs to include <stdio.> (deprecated)


    Express typing lets me down again, <stdio.h>.



    Brian
    Default User, Oct 19, 2004
    #9
  10. Matt wrote:
    > Do we have sscanf feature in C++? I guess since C++ is superset of C.
    > So the following are valid C++ code. Is that true?
    >
    > char* buf = "10:25:33";
    > sscanf(buf, "%d:%d:%d", &h, &m, &s);
    >
    > Please advise. thanks!!



    This one is perfectly valid C++ code:


    #include <stdio.h>

    int main()
    {
    int h, m ,s;

    char* buf = "10:25:33";

    sscanf(buf, "%d %d %d", &h, &m, &s);
    }



    With very few exceptions (meaning differences), C++ retains C90 as a subset.



    As the C++ standard mentions:


    "C++ is a general purpose programming language based on the C
    programming language as described in ISO/IEC 9899:1990 Programming
    languages – C (1.2). In addition to the facilities provided by C, C++
    provides additional data types, classes, templates, exceptions,
    namespaces, inline functions, operator overloading, function name
    overloading, references, free store management operators, and additional
    library facilities."



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Oct 19, 2004
    #10
  11. > This one is perfectly valid C++ code:
    >
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > int h, m ,s;
    >
    > char* buf = "10:25:33";
    >
    > sscanf(buf, "%d %d %d", &h, &m, &s);
    > }



    I meant this:


    #include <stdio.h>

    int main()
    {
    int h, m ,s;

    char* buf = "10 25 33";

    sscanf(buf, "%d %d %d", &h, &m, &s);

    return 0;
    }


    Why are you using these ':' anyway?



    > With very few exceptions (meaning differences), C++ retains C90 as a
    > subset.
    >
    >
    >
    > As the C++ standard mentions:
    >
    >
    > "C++ is a general purpose programming language based on the C
    > programming language as described in ISO/IEC 9899:1990 Programming
    > languages – C (1.2). In addition to the facilities provided by C, C++
    > provides additional data types, classes, templates, exceptions,
    > namespaces, inline functions, operator overloading, function name
    > overloading, references, free store management operators, and additional
    > library facilities."




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Oct 19, 2004
    #11
  12. Matt

    Mike Wahler Guest

    "Howard" <> wrote in message
    news:xsXcd.718672$...
    >
    > "Mike Wahler" <> wrote in message
    > news:VeXcd.621$%...
    > > "Howard" <> wrote in message
    > > news:gzWcd.8966$...
    > >>
    > >> "Matt" <> wrote in message
    > >> news:...
    > >> > Do we have sscanf feature in C++? I guess since C++ is superset of C.
    > >> > So the following are valid C++ code. Is that true?
    > >> >
    > >> > char* buf = "10:25:33";
    > >> > sscanf(buf, "%d:%d:%d", &h, &m, &s);
    > >> >
    > >> > Please advise. thanks!!
    > >>
    > >> I don't see it, either in my headers or in my books.

    > >
    > > You should find declarations of all the C(90) library functions
    > > in your headers, or your C++ implementation is not complete.
    > >

    >
    > Ah, you are correct. I didn't have the path to those included in my

    search
    > paths, I guess. When I searched the whole source tree, I found it in
    > cstdio.


    For completeness, I must add:

    You may not have found what you did, and still have a conforming
    implementation. There's no requirement for the declarations of
    headers to be stored in files at all. They could e.g. be provided
    directly by the compiler itself (but I've never seen this done).

    The requirement is that when you write:

    #include <headername>

    the declarations that the standard specifies that the given
    header provides must be visible in the scope in which the
    #include statement appears.

    -Mike
    Mike Wahler, Oct 19, 2004
    #12
  13. Matt

    Mike Wahler Guest

    "Mike Wahler" <> wrote in message
    news:QzZcd.498$...
    > For completeness, I must add:
    >
    > You may not have found what you did, and still have a conforming
    > implementation. There's no requirement for the declarations of
    > headers to be stored in files at all. They could e.g. be provided
    > directly by the compiler itself (but I've never seen this done).
    >
    > The requirement is that when you write:
    >
    > #include <headername>
    >
    > the declarations that the standard specifies that the given
    > header provides must be visible in the scope in which the
    > #include statement appears.



    which of course means my original assertion:

    > > > You should find declarations of all the C(90) library functions
    > > > in your headers, or your C++ implementation is not complete.


    was incorrect. The information might not have been accessible to you,
    although it was still there.

    Oops, sorry .:)

    -Mike
    Mike Wahler, Oct 19, 2004
    #13
  14. Mike Wahler wrote:

    > For completeness, I must add:
    >
    > You may not have found what you did, and still have a conforming
    > implementation. There's no requirement for the declarations of
    > headers to be stored in files at all. They could e.g. be provided
    > directly by the compiler itself (but I've never seen this done).
    >
    > The requirement is that when you write:
    >
    > #include <headername>
    >
    > the declarations that the standard specifies that the given
    > header provides must be visible in the scope in which the
    > #include statement appears.



    Yes, standard library header files may not really exist as files, and
    #include statements for these to work as switches enabling specific
    functionality.



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Oct 19, 2004
    #14
  15. Matt

    Gerhard Wesp Guest

    Matt <> wrote:
    > char* buf = "10:25:33";
    > sscanf(buf, "%d:%d:%d", &h, &m, &s);


    You should definitely use char const* buf = ... here. The dangerous
    implicit conversion removing the const here will most likely go away in
    the future.

    Otherwise, using sscanf() is perfectly OK in C++, although other
    techniques may be safer :)

    Cheers
    -Gerhard
    --
    Gerhard Wesp o o Tel.: +41 (0) 43 5347636
    Bachtobelstrasse 56 | http://www.cosy.sbg.ac.at/~gwesp/
    CH-8045 Zuerich \_/ See homepage for email address!
    Gerhard Wesp, Oct 19, 2004
    #15
  16. Matt

    Default User Guest

    Ioannis Vranos wrote:

    > > This one is perfectly valid C++ code:
    > >
    > >
    > > #include <stdio.h>
    > >
    > > int main()
    > > {
    > > int h, m ,s;
    > >
    > > char* buf = "10:25:33";
    > >
    > > sscanf(buf, "%d %d %d", &h, &m, &s);
    > > }

    >
    >
    > I meant this:
    >
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > int h, m ,s;
    >
    > char* buf = "10 25 33";
    >
    > sscanf(buf, "%d %d %d", &h, &m, &s);


    That's not the same problem.


    > Why are you using these ':' anyway?


    Would it help if he named the variables hour, minute, second?



    Brian
    Default User, Oct 19, 2004
    #16
    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. Anonymous
    Replies:
    0
    Views:
    466
    Anonymous
    Oct 30, 2003
  2. pramod
    Replies:
    22
    Views:
    1,793
    Lew Pitcher
    Jan 6, 2004
  3. Mike

    sscanf and c++!

    Mike, Jun 8, 2004, in forum: C++
    Replies:
    3
    Views:
    3,534
  4. Darrell Grainger

    Re: Unitialized Memory Read In sscanf()

    Darrell Grainger, Jun 24, 2003, in forum: C Programming
    Replies:
    1
    Views:
    809
    Alan Balmer
    Jun 24, 2003
  5. Matt

    sscanf feature in C++?

    Matt, Oct 18, 2004, in forum: C Programming
    Replies:
    9
    Views:
    420
    Default User
    Oct 19, 2004
Loading...

Share This Page