fscanf equivalent in c++

J

John Phung

Is there a fscanf equivalent for c++? Here's what I'm talking about:

unsigned int NextAddress(ifstream& AddressFile) {
unsigned int next_address;

-->How do I rewrite the fscanf listed below using ifstream?
-->fscanf(AddressFile, "%x", next_address);

if(AddressFile.eof()) {
return 0;
}
return next_address;

} //Return the next address being read from a file in the main program
 
R

Rolf Magnus

John said:
Is there a fscanf equivalent for c++?

There is fscanf. ;-)
Here's what I'm talking about:

unsigned int NextAddress(ifstream& AddressFile) {
unsigned int next_address;

-->How do I rewrite the fscanf listed below using ifstream?
-->fscanf(AddressFile, "%x", next_address);

AddressFile >> std::hex >> next_address;
if(AddressFile.eof()) {
return 0;

What if there is any other special condition than eof?
 
K

Kevin Goodsell

John said:
Is there a fscanf equivalent for c++? Here's what I'm talking about:

The equivalent of fscanf() in C++ is fscanf(). But don't use it.
unsigned int NextAddress(ifstream& AddressFile) {
unsigned int next_address;

-->How do I rewrite the fscanf listed below using ifstream?
-->fscanf(AddressFile, "%x", next_address);

And here's why you shouldn't use it. If you used this code, your program
would be severely broken. The %x format specifier expects the
corresponding argument to be of type "pointer to unsigned int". You
passed the wrong type, and invoked undefined behavior.

The scanf and printf families of functions don't provide reasonable
type-checking, and are horribly error-prone and very dangerous to use.
That's why C++ provides better alternatives in the form of stream classes.

Anything that puts the burden of type-checking on the programmer should
be avoided. Such things should be used only when absolutely necessary,
and then only with great caution by someone who know what they are doing.

Boost also provides a type-safe library for printf()-like formatting:

http://www.boost.org/libs/format/index.html

Although it does not appear to provide anything scanf()-like.

-Kevin
 
K

Kevin Goodsell

Nick said:
what about

fscanf(AddressFile, "%i", next_address);

Undefined behavior. %i requires the corresponding argument to be of type
"pointer to (signed) int". You have provided an argument of type
"unsigned int".

As I said in my other reply in this thread:

The scanf and printf families of functions don't provide reasonable
type-checking, and are horribly error-prone and very dangerous to use.
That's why C++ provides better alternatives in the form of stream classes.

Anything that puts the burden of type-checking on the programmer should
be avoided. Such things should be used only when absolutely necessary,
and then only with great caution by someone who know what they are doing.

-Kevin
 
R

Richard Herring

Kevin said:
Undefined behavior. %i requires the corresponding argument to be of
type "pointer to (signed) int". You have provided an argument of type
"unsigned int".

I suspect that's not his point, which might better have been stated as
"'what's the ifstream equivalent of fscanf(..,. "%i", ...) ?" Note that
it's %i, not %d.

Or to put it anther way, "is there something which will read any of the
strings 012, 10 and 0x0a giving the same result in each case?"
 
K

Kevin Goodsell

Richard said:
I suspect that's not his point, which might better have been stated as
"'what's the ifstream equivalent of fscanf(..,. "%i", ...) ?" Note that
it's %i, not %d.

Or to put it anther way, "is there something which will read any of the
strings 012, 10 and 0x0a giving the same result in each case?"

Yes, I see what you mean now. Sorry about the misunderstanding.

Off the top of my head, I don't know of a way to do this with streams
(short of writing your own code to check the initial character(s)). A
quick glance at the list of stream manipulators doesn't reveal anything
that looks likely to accomplish this.

-Kevin
 
P

P.J. Plauger

Yes, I see what you mean now. Sorry about the misunderstanding.

Off the top of my head, I don't know of a way to do this with streams
(short of writing your own code to check the initial character(s)). A
quick glance at the list of stream manipulators doesn't reveal anything
that looks likely to accomplish this.

Set basefield to zero and you'll convert integer input with %i.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
N

Nick Hounsome

P.J. Plauger said:
Set basefield to zero and you'll convert integer input with %i.

I would like you to be right (and you may well be for dinkum) but 27.4.2.1.2
table 84
lists the allowable values for basefield and 0 isn't one of them.
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top