什么时候应该使用 fputs 而不是 fprintf?
两者到底有什么区别?
What exactly is the difference between the two?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
两者到底有什么区别?
What exactly is the difference between the two?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
fprintf 进行格式化输出。也就是说,它读取并解释一个
您提供的格式字符串并将其写入输出流
结果。
fputs 只是将您提供的字符串写入指定的输出
溪流。
fputs()
不必解析输入字符串来确定您想要做的只是打印字符串。fprintf()
允许您在当时进行格式化的输出。fprintf does formatted output. That is, it reads and interprets a
format string that you supply and writes to the output stream the
results.
fputs simply writes the string you supply it to the indicated output
stream.
fputs()
doesn't have to parse the input string to figure out that all you want to do is print a string.fprintf()
allows you to format at the time of outputting.正如其他评论者所指出的(并且从文档中可以明显看出),最大的区别是
printf
允许参数格式化。也许您会问,在没有向 printf() 传递额外参数的情况下,这些函数是否等效?嗯,他们不是。
第二个可能是错误的,因为
fprintf()
的字符串参数仍然是格式化字符串:如果它有“%”字符,它将被解释为格式化说明符。功能上等效的(但不太直接/高效/好的)形式是
As have been pointed out by other commenters (and as it's obvious from the docs) the great difference is that
printf
allows formatting of arguments.Perhaps you are asking if the functions are equivalent where no additional arguments are passed to
printf()
? Well, they are not.The second is probably wrong, because the string argument to
fprintf()
is a still a formating string: if it has a '%' character it will be interpreted as a formatting specifier.The functionally equivalent (but less direct/efficient/nice) form would be
嗯……
...
puts()
只是写入一个字符串,而printf()
有许多针对多种数据类型的格式化工具。fputs()
http://www.cplusplus.com/reference/clibrary/cstdio/fputs/
fprintf()
http://www.cplusplus.com/reference/clibrary/cstdio/fprintf/
文档很有用!学会阅读它,你就会拥有一个强大的工具。
Uhm...
...
puts()
just writes a string, whileprintf()
has a number of formatting facilities for several types of data.fputs()
http://www.cplusplus.com/reference/clibrary/cstdio/fputs/
fprintf()
http://www.cplusplus.com/reference/clibrary/cstdio/fprintf/
Documentation is useful! Learn to read it, and you'll have a powerful tool on your side.