John Phung wrote:
> 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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.