473,323 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

fscanf equivalent in c++

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
Jul 22 '05 #1
22 4909
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

Jul 22 '05 #2
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

Jul 22 '05 #3
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.
Jul 22 '05 #4
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.
Jul 22 '05 #5

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c4*************@news.t-online.com...
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);

???
Jul 22 '05 #6

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c4*************@news.t-online.com...
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);

???
Jul 22 '05 #7
Nick Hounsome wrote:
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c4*************@news.t-online.com...
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.
Jul 22 '05 #8
Nick Hounsome wrote:
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c4*************@news.t-online.com...
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.
Jul 22 '05 #9
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.

Jul 22 '05 #10
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.

Jul 22 '05 #11
In message <wj**************@newsread2.news.pas.earthlink.net >, Kevin
Goodsell <us*********************@neverbox.com> writes
Nick Hounsome wrote:
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c4*************@news.t-online.com...
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
Jul 22 '05 #12
In message <wj**************@newsread2.news.pas.earthlink.net >, Kevin
Goodsell <us*********************@neverbox.com> writes
Nick Hounsome wrote:
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c4*************@news.t-online.com...
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
Jul 22 '05 #13
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.
Jul 22 '05 #14
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.
Jul 22 '05 #15
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:oM*****************@newsread1.news.pas.earthl 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
Jul 22 '05 #16
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:oM*****************@newsread1.news.pas.earthl 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
Jul 22 '05 #17
"P.J. Plauger" wrote:
<snip>

Hey, congrats on your recent DDJ award!
Jul 22 '05 #18
"P.J. Plauger" wrote:
<snip>

Hey, congrats on your recent DDJ award!
Jul 22 '05 #19
"Julie" <ju***@nospam.com> wrote in message
news:40***************@nospam.com...
"P.J. Plauger" wrote:
<snip>

Hey, congrats on your recent DDJ award!


Thanks. My first, and probably last, stint as a cover model.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #20
"Julie" <ju***@nospam.com> wrote in message
news:40***************@nospam.com...
"P.J. Plauger" wrote:
<snip>

Hey, congrats on your recent DDJ award!


Thanks. My first, and probably last, stint as a cover model.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #21

"P.J. Plauger" <pj*@dinkumware.com> wrote in message
news:Pz******************@nwrddc02.gnilink.net...
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:oM*****************@newsread1.news.pas.earthl 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.


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.
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com

Jul 22 '05 #22

"P.J. Plauger" <pj*@dinkumware.com> wrote in message
news:Pz******************@nwrddc02.gnilink.net...
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:oM*****************@newsread1.news.pas.earthl 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.


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.
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com

Jul 22 '05 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: John Phung | last post by:
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...
4
by: Psibur | last post by:
Hello, trying to get back into c and was having issue with reading a simple text file with an aribtrary # of lines with 3 int's per line, with the eventual purpose of putting each int into an...
1
by: siliconwafer | last post by:
Hi All, here is one code: int main() { FILE*fp; unsigned long a; fp = fopen("my_file.txt","w+"); a = 24; fprintf(fp,"%ld",a); while(fscanf(fp,"%ld",&a) == 1) {
37
by: PeterOut | last post by:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2). I am not sure if this is a C, C++ or MS issue but fscanf has been randomly hanging on me. I make the call hundreds, if not thousands, of...
6
by: InuY4sha | last post by:
Hi, I hope to be not off topic.. I have a string on a file "00:32:43:54:A2:2D" (let's say a MAC address) I use fscanf like this fscanf( file, "%02X:%02X:%02X:%02X:%02X:%02X", ptr, ptr+1, ptr+2,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.