Is there a fscanf equivalent for c++? Here's what I'm talking about:
unsigned int NextAddress(ifs tream& AddressFile) {
unsigned int next_address;
-->How do I rewrite the fscanf listed below using ifstream?
-->fscanf(Address File, "%x", next_address);
if(AddressFile. eof()) {
return 0;
}
return next_address;
} //Return the next address being read from a file in the main program 22 4969
John Phung wrote: Is there a fscanf equivalent for c++?
There is fscanf. ;-)
Here's what I'm talking about:
unsigned int NextAddress(ifs tream& AddressFile) { unsigned int next_address;
-->How do I rewrite the fscanf listed below using ifstream? -->fscanf(Address File, "%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
John Phung wrote: Is there a fscanf equivalent for c++?
There is fscanf. ;-)
Here's what I'm talking about:
unsigned int NextAddress(ifs tream& AddressFile) { unsigned int next_address;
-->How do I rewrite the fscanf listed below using ifstream? -->fscanf(Address File, "%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
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(ifs tream& AddressFile) { unsigned int next_address;
-->How do I rewrite the fscanf listed below using ifstream? -->fscanf(Address File, "%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.
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(ifs tream& AddressFile) { unsigned int next_address;
-->How do I rewrite the fscanf listed below using ifstream? -->fscanf(Address File, "%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.
"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(ifs tream& AddressFile) { unsigned int next_address;
-->How do I rewrite the fscanf listed below using ifstream? -->fscanf(Address File, "%x", next_address);
AddressFile >> std::hex >> next_address;
what about
fscanf(AddressF ile, "%i", next_address);
???
"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(ifs tream& AddressFile) { unsigned int next_address;
-->How do I rewrite the fscanf listed below using ifstream? -->fscanf(Address File, "%x", next_address);
AddressFile >> std::hex >> next_address;
what about
fscanf(AddressF ile, "%i", next_address);
???
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(ifs tream& AddressFile) { unsigned int next_address;
-->How do I rewrite the fscanf listed below using ifstream? -->fscanf(Address File, "%x", next_address);
AddressFile >> std::hex >> next_address;
what about
fscanf(AddressF ile, "%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.
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(ifs tream& AddressFile) { unsigned int next_address;
-->How do I rewrite the fscanf listed below using ifstream? -->fscanf(Address File, "%x", next_address);
AddressFile >> std::hex >> next_address;
what about
fscanf(AddressF ile, "%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.
Nick Hounsome wrote: > -->How do I rewrite the fscanf listed below using ifstream?
^^^^^^^^^^^^^^
> -->fscanf(Address File, "%x", next_address);
AddressFile >> std::hex >> next_address;
what about
fscanf(AddressF ile, "%i", next_address);
???
The OP wanted to use an ifstream. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 using ifstream?
-->fscanf(AddressFile, "%x", next_address);
if(AddressFile.eof()) {
return 0;
|
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 element of an
array (eventually will be other things, but I'm sticking to int's for
now). I.e.:
0 1 1
1 1 1
2 1 1 etc...
The problem is it'll read...
|
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) {
|
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 times but it hangs in different places with the same data. The
offending code follows.
ReadFile(char *csFileName)
{
float fFloat1, fFloat2;
|
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,
ptr3, ptr+4, ptr+5);
It works, but I get warnings as the expected size is int while I'm
storing onto a uint8_t array.. is there a format to store...
| |
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
| |
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |