Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sscanf feature in C++?

Reply
Thread Tools

sscanf feature in C++?

 
 
Matt
Guest
Posts: n/a
 
      10-18-2004
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!!
 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      10-18-2004

"Matt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      10-18-2004

"Matt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-18-2004
"Howard" <(E-Mail Removed)> wrote in message
news:gzWcd.8966$(E-Mail Removed)...
>
> "Matt" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-18-2004

"Matt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      10-18-2004

"Mike Wahler" <(E-Mail Removed)> wrote in message
news:VeXcd.621$%(E-Mail Removed) k.net...
> "Howard" <(E-Mail Removed)> wrote in message
> news:gzWcd.8966$(E-Mail Removed)...
>>
>> "Matt" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) om...
>> > 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


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      10-18-2004
John Harrison wrote:

>
> "Matt" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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

 
Reply With Quote
 
Karthik Kumar
Guest
Posts: n/a
 
      10-18-2004
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. '
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      10-18-2004
Default User wrote:

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


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



Brian

 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      10-18-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sscanf feature in C++? Matt C Programming 9 10-19-2004 08:39 PM
sscanf and c++! Mike C++ 3 06-09-2004 10:46 AM
endianness and sscanf/sprintf pramod C++ 22 01-06-2004 01:02 PM
Re: %n conversion in sscanf/VC++ 6-solved Anonymous C++ 0 10-30-2003 05:05 PM
Re: Unitialized Memory Read In sscanf() Darrell Grainger C Programming 1 06-24-2003 06:52 PM



Advertisments