Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > fscanf equivalent in c++

Reply
Thread Tools

fscanf equivalent in c++

 
 
John Phung
Guest
Posts: n/a
 
      04-06-2004
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
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      04-06-2004
John Phung wrote:

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

> }
> return next_address;
>
> } //Return the next address being read from a file in the main program


--
"Time flies like an arrow. Fruit flies like a banana."
- Groucho Marx

 
Reply With Quote
 
 
 
 
Kevin Goodsell
Guest
Posts: n/a
 
      04-06-2004
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.
 
Reply With Quote
 
Nick Hounsome
Guest
Posts: n/a
 
      04-07-2004

"Rolf Magnus" <> wrote in message
news:c4tse0$dqk$06$...
> John Phung wrote:
>
> > 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;


what about

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

???


 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      04-07-2004
Nick Hounsome wrote:

> "Rolf Magnus" <> wrote in message
> news:c4tse0$dqk$06$...
>
>>John Phung wrote:
>>
>>>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;

>
>
> 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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      04-07-2004
Nick Hounsome wrote:

>> > -->How do I rewrite the fscanf listed below using ifstream?

^^^^^^^^^^^^^^

>> > -->fscanf(AddressFile, "%x", next_address);

>>
>> AddressFile >> std::hex >> next_address;

>
> what about
>
> fscanf(AddressFile, "%i", next_address);
>
> ???


The OP wanted to use an ifstream.

 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      04-08-2004
In message <wjYcc.326$ >, Kevin
Goodsell <> writes
>Nick Hounsome wrote:
>
>> "Rolf Magnus" <> wrote in message
>> news:c4tse0$dqk$06$...
>>
>>>John Phung wrote:
>>>
>>>>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;

>> 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".


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

--
Richard Herring
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      04-08-2004
Richard Herring wrote:
>
> 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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
 
Reply With Quote
 
P.J. Plauger
Guest
Posts: n/a
 
      04-08-2004
"Kevin Goodsell" <> wrote in message
newsMfdc.1832$ ink.net...

> Richard Herring wrote:
> >
> > 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.


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

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


 
Reply With Quote
 
Julie
Guest
Posts: n/a
 
      04-08-2004
"P.J. Plauger" wrote:
<snip>

Hey, congrats on your recent DDJ award!
 
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
Question about fscanf .. Marc Reclaire C++ 2 12-18-2003 12:51 PM
How to use ifstream::get() like fscanf()? cylin C++ 7 09-19-2003 05:42 AM
Re: How to put comments in initialization file read by fscanf()? David Rubin C Programming 0 07-14-2003 09:14 PM
Re: How to put comments in initialization file read by fscanf()? Chris Torek C Programming 0 07-14-2003 05:33 AM
fscanf question Fallon C Programming 3 07-13-2003 05:55 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57