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

Howard

Matt said:
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
 
J

John Harrison

Matt said:
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
 
M

Mike Wahler

Howard said:
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
 
M

Mike Wahler

Matt said:
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
 
H

Howard

Mike Wahler said:
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
 
D

Default User

John said:
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
 
K

Karthik Kumar

Matt said:
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.
 
I

Ioannis Vranos

Matt said:
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."
 
I

Ioannis Vranos

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?
 
M

Mike Wahler

Howard said:
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
 
M

Mike Wahler

Mike Wahler said:
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:

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

Oops, sorry .:)

-Mike
 
I

Ioannis Vranos

Mike said:
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.
 
G

Gerhard Wesp

Matt said:
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
 
D

Default User

Ioannis said:
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
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top