问题描述
c ++有fscanf等价吗?这就是我在说什么 :
unsigned int NextAddress(ifstream& AddressFile){
unsigned int next_address;
- >如何使用ifstream重写下面列出的fscanf?
- > fscanf(AddressFile,"%x" ,next_address);
if(AddressFile.eof()){
返回0;
}
返回next_address;
} //返回从主程序中的文件中读取的下一个地址
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
推荐答案
-
时间过得像箭一样。果蝇像香蕉一样苍蝇。
- Groucho Marx
--
"Time flies like an arrow. Fruit flies like a banana."
- Groucho Marx
-
时间过得像箭一样。果蝇像香蕉一样苍蝇。
- Groucho Marx
--
"Time flies like an arrow. Fruit flies like a banana."
- Groucho Marx
这就是你的原因不应该使用它。如果您使用此代码,您的程序
将严重破坏。 %x格式说明符期望
对应的参数类型为指向unsigned int的类型。你
传递了错误的类型,并调用了未定义的行为。
scanf和printf系列函数不提供合理的
类型检查,并且非常容易出错且使用起来非常危险。
这就是为什么C ++以流类的形式提供更好的替代方案。
任何将类型检查负担放在程序员身上的东西应该避免
。只有在绝对必要时才应该使用这些东西,然后只有在知道自己在做什么的人时才会非常谨慎。
Boost还提供了一种类型 - printf()的安全库 - 类似格式化:
虽然它似乎没有提供任何scanf() - 就像。
-Kevin
-
我的电子邮件地址有效,但会定期更改。
要联系我,请使用最近发布的地址。
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.
这篇关于fscanf相当于c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!