什么时候应该使用 fputs 而不是 fprintf?

发布于 2024-11-01 16:36:26 字数 17 浏览 0 评论 0原文

两者到底有什么区别?

What exactly is the difference between the two?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心如荒岛 2024-11-08 16:36:26

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.

是伱的 2024-11-08 16:36:26

正如其他评论者所指出的(并且从文档中可以明显看出),最大的区别是 printf 允许参数格式化。

也许您会问,在没有向 printf() 传递额外参数的情况下,这些函数是否等效?嗯,他们不是。

   char * str;
   FILE * stream;
   ...
   fputs(str,stream);    // this is NOT the same as the following line
   fprintf(stream,str);  // this is probably wrong

第二个可能是错误的,因为 fprintf() 的字符串参数仍然是格式化字符串:如果它有“%”字符,它将被解释为格式化说明符。

功能上等效的(但不太直接/高效/好的)形式是

   fprintf(stream,"%s", str);  

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.

   char * str;
   FILE * stream;
   ...
   fputs(str,stream);    // this is NOT the same as the following line
   fprintf(stream,str);  // this is probably wrong

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

   fprintf(stream,"%s", str);  
离鸿 2024-11-08 16:36:26

嗯……
...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, while printf() 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文