sscanf feature in C++?

M

Matt

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!!
 
D

Dave Vandervies

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!!

Since this is comp.lang.c, the best answer we can give you is "This is
perfectly valid C".

If you go ask this in comp.lang.c++, they'll tell you that it's also
perfectly valid C++ (assuming you have an appropriate using directive in
scope or use the C-style header names instead of the C++-style header
names), and possibly also mention std::strstream as a more C++-ish
alternative.


dave

--
Dave Vandervies (e-mail address removed)
May I be excused for showing incomplete/incorrect knowledge of C++ knowledge
in a C newsgroup? --Michael Rubenstein and Ian Woods in comp.lang.c
 
M

Martin Ambuhl

Matt said:
Do we have sscanf feature in C++?

"We", in comp.lang.c, don't, because we program in C. The folks in
comp.lang.c++ would tell you that they have sscanf, but many are likely
to tell you to use some C++ "feature" instead.
I guess since C++ is superset of C.

Your premise is wrong. C++ is not a superset of C. Whoever told you
that should be ignored: he is a fool.
So the following are valid C++ code.

We don't know: comp.lang.c doesn't do C++. But..
Is that true?

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

This legal C program

#include <stdio.h>

int main(void)
{
char *buf = "10:25:33";
int h, m, s;
sscanf(buf, "%d:%d:%d", &h, &m, &s);
printf("%d hours + %d minutes + %d seconds\n", h, m, s);
return 0;
}

produces this output
10 hours + 25 minutes + 33 seconds

and this probably legal (but check comp.lang.c++ where they do C++) C++
program

#include <cstdio>

int main()
{
char *buf = "10:25:33";
int h, m, s;
sscanf(buf, "%d:%d:%d", &h, &m, &s);
printf("%d hours + %d minutes + %d seconds\n", h, m, s);
}

produces this output
10 hours + 25 minutes + 33 seconds
 
D

Default User

Matt said:
Do we have sscanf feature in C++?

This is comp.lang.c. You want comp.lang.c++.
I guess since C++ is superset of C.

This is a false statement.
So the following are valid C++ code. Is that true?

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

<OT>
The C standard library, for the most part, is included in C++.
</OT>


Brian
 
K

Keith Thompson

Martin Ambuhl said:
Matt wrote: [...]
I guess since C++ is superset of C.

Your premise is wrong. C++ is not a superset of C. Whoever told you
that should be ignored: he is a fool.

Yes, but C++ is very nearly a superset of C (specifically C90). In
particular, it includes the C90 standard library, including the sscanf
function.

You can't expect an arbitrary C program to be a valid C++ program with
the same semantics, but the intersection of the two languages is quite
large, and not much smaller than C itself.
 
D

Default User

P.J. Plauger said:
Which parts are left out?


What I meant was, "for the most part without alteration". I'm not 100%
sure there aren't some slight syntax/semantic differences due to the
differences between the two languages.

I was merely hedging my bets slightly. You'd know, of course.




Brian
 
C

CBFalconer

P.J. Plauger said:
Which parts are left out?

A shining example of "insert knife slowly and gently, then twist".
:)

--
"I support the Red Sox and any team that beats the Yankees"
"Any baby snookums can be a Yankee fan, it takes real moral
fiber to be a Red Sox fan"
"I listened to Toronto come back from 3:0 in '42, I plan to
watch Boston come back from 3:0 in 04"
 
D

Dan Pop

In said:
Which parts are left out?

Most of the bits that are new in C99 are not in the standard C++ library.

4 Except as noted in clauses 18 through 27, the contents of each header
cname shall be the same as that of the corresponding header name.h,
as specified in ISO/IEC 9899:1990 Programming Languages C (Clause
^^^^
7), or ISO/IEC:1990 Programming LanguagesC AMENDMENT 1: C Integrity,
(Clause 7), as appropriate, as if by inclusion. In the C++ Standard
Library, however, the declarations and definitions (except for names
which are defined as macros in C) are within namespace scope (3.3.5)
of the namespace std.

Dan
 
D

Default User

CBFalconer said:
A shining example of "insert knife slowly and gently, then twist".

I already replied to that. It was a hedge for a possible gap in my
knowledge of C++. Reasonable under the circumstances and locale, I'd
say.



Brian
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,156
Latest member
KetoBurnSupplement
Top