473,474 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 4948
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.