C++官网参考链接:https://cplusplus.com/reference/cstdio/fprintf/

函数 
<cstdio>
fprintf
int fprintf ( FILE * stream, const char * format, ... );
将格式化的数据写入流
将按format指向的C字符串写入stream。如果format包含格式说明符(以%开始的子序列),则format后面的附加实参将被格式化并插入结果字符串中,取代它们各自的说明符。
format形参之后,函数期望至少有与format指定的相同数量的附加实参。

形参
stream 
指向标识输出流的FILE对象的指针。
format 
C字符串,包含要写入stream的文本。
它可以有选择地包含嵌入的格式说明符,这些说明符将被后续附加实参中指定的值替换并按要求格式化。
格式说明符遵循这个原型:
%[flags][width][.precision][length]specifier
其中,结束的specifier字符是最重要的组件,因为它定义了对应实参的类型和解释:

specifier Output Example
or i Signed decimal integer 392
u Unsigned decimal integer 7235
o Unsigned octal 610
x Unsigned hexadecimal integer 7fa
X Unsigned hexadecimal integer (uppercase) 7FA
f Decimal floating point, lowercase 392.65
F Decimal floating point, uppercase 392.65
e Scientific notation (mantissa/exponent), lowercase 3.9265e+2
E Scientific notation (mantissa/exponent), uppercase 3.9265E+2
g Use the shortest representation: %e or %f 392.65
G Use the shortest representation: %E or %F 392.65
a Hexadecimal floating point, lowercase -0xc.90fep-2
A Hexadecimal floating point, uppercase -0XC.90FEP-2
c Character a
s String of characters sample
p Pointer address b8000000
n

Nothing printed.
The corresponding argument must be a pointer to a signed int.
The number of characters written so far is stored in the pointed location.

(不打印出来。

对应的实参必须是指向signed int的指针。

到目前为止写入的字符数存储在指向的位置。)

% A % followed by another % character will write a single % to the stream. %

格式说明符还可以包含子说明符:flags、width、.precision和modifiers(按此顺序),它们是可选的,并遵循以下规范:

flags description
- Left-justify within the given field width; Right justification is the default (see width sub-specifier).(在给定的字段宽度内左对齐;右对齐是默认的(参见width子说明符)。)
+

Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.

(甚至对于正数,强制在结果前面加上正号或负号(+或-)。默认情况下,只有负数前面有-号。)

(space)

If no sign is going to be written, a blank space is inserted before the value.

(如果不写入符号,则在值之前插入一个空白。)

#

Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero.
Used with a, A, e, E, f, F, g or G it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written.

(与o,x或X说明符一起使用时,对于不等于0的值,该值的前面分别加0,0x或0X。

与a,A,e,E,f,F,g或G一起使用,它强制书面输出包含小数点,即使后面没有其他数字。默认情况下,如果后面没有数字,则不写入小数点。)

0

Left-pads the number with zeroes (0) instead of spaces when padding is specified (see width sub-specifier).

(当指定填充时,用零(0)代替空白左填充数字(参见width子说明符)。)

width description
(number)

Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.

(要打印的最小字符数。如果要打印的值短于此数字,结果将用空白填充。即使结果更大,该值也不会被截断。)

*

The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

(width不是在format字符串中指定的,而是作为必须格式化的实参之前的一个附加整数值实参。)

.precision description
.number

For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.
For a, A, e, E, f and F specifiers: this is the number of digits to be printed after the decimal point (by default, this is 6).
For g and G specifiers: This is the maximum number of significant digits to be printed.
For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.
If the period is specified without an explicit value for precision, 0 is assumed.

(对于整数说明符(d,i,o,u,x,X):precision指定要写入的最小位数。如果要写入的值短于此数字,则用前导0填充结果。即使结果较长,该值也不会被截断。precision为0意味着没有为值0写入字符。

对于a,A,e,E,f和F说明符:这是小数点后要打印的位数(默认情况下,这是6)。

对于g和G说明符:这是要打印的有效数字的最大数量。

对于s:这是要打印的最大字符数。默认情况下,打印所有字符,直到遇到结束的空字符。

如果指定的周期没有明确的precision值,则假定为0。)

.*

The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

(precision不是在format字符串中指定的,而是作为必须格式化的实参前面的附加整数值实参指定的。)

length子说明符修改数据类型的长度。这是一个图表,显示了用于解释有或没有length说明符的对应实参的类型(如果使用了不同的类型,在允许的情况下,执行适当的类型提升或转换):

specifiers
length d i u o x X f F e E g G a A c s p n
(none) int unsigned int double int char* void* int*
hh signed char unsigned char signed char*
h short int unsigned short int short int*
l long int unsigned long int wint_t wchar_t* long int*
ll long long int unsigned long long int long long int*
j intmax_t uintmax_t intmax_t*
z size_t size_t size_t*
t ptrdiff_t ptrdiff_t ptrdiff_t*
L long double

注意,c说明符接受int(或wint_t)作为实参,但在格式化输出之前执行到char值(或wchar_t)的适当转换。
注:黄色行表示C99引入的说明符和子说明符。有关扩展类型的说明符,请参见<cinttypes>。
...(附加实参) 
根据format字符串的不同,函数可能需要一系列附加实参,每个实参包含一个值,用于替换format字符串中的格式说明符(对于n是指向存储位置的指针)。
这些实参的数量至少应该与格式说明符中指定的值的数量相同。函数将忽略额外的实参。

返回值
如果成功,则返回写入的字符总数。
如果发生写错误,则设置错误指示符(ferror)并返回负数。
如果在写入宽字符时发生多字节字符编码错误,则errno设置为EILSEQ并返回负数。

用例
/* fprintf example */
#include <stdio.h>

int main ()
{
   FILE * pFile;
   int n;
   char name [100];

pFile = fopen ("myfile.txt","w");
   for (n=0 ; n<3 ; n++)
   {
     puts ("please, enter a name: ");
     gets (name);
     fprintf (pFile, "Name %d [%-10.10s]\n",n+1,name);
   }
   fclose (pFile);

return 0;
}
这个示例提示用户输入一个名称3次,然后将它们写入myfile.txt,每一行都具有固定的长度(总共19个字符+换行符)。
使用了两个格式标签: 
%d:带符号的十进制整数
%-10.10s:左对齐(-),最少十个字符(10),最多十个字符(.10),字符串(s)。
假设我们输入了John,Jean-Francois和Yoko作为三个名字,myfile.txt将包含:
Name 1 [John      ] 
Name 2 [Jean-Franc] 
Name 3 [Yoko      ]
有关格式化的更多示例,请参阅printf。
输出:

兼容性
特定的库实现可能支持额外的说明符和子说明符。
这里列出的是由最新的C和C++标准支持的(都是在2011年发布的),但是黄色的那些是在C99中引入的(只有C++11之后的C++实现才需要),并且可能不被遵循旧标准的库所支持。

C++ Reference: Standard C++ Library reference: C Library: cstdio: fprintf相关推荐

  1. Conditional project or library reference in Visual Studio

    Conditional project or library reference in Visual Studio In case you were wondering why you haven't ...

  2. link library 、target library、symbol library、synthetic library对照分析-基础小知识(九)

    文章目录 1.1 概念介绍 1.2 疑问解析 参考文档 DC 应用过程中涉及link library .target library.symbol library.synthetic library, ...

  3. undefined reference to `__stack_chk_guard' .. undefined reference to `__stack_chk_fail'

    1. 编译出错 undefined reference to `__stack_chk_guard' undefined reference to `__stack_chk_fail' 解决方法-1: ...

  4. git fatal: cannot lock ref ‘HEAD‘:unable to resolve reference‘refs/heads/main‘:reference broken

    问题: push的时候,电脑强制关机再启动,vscode中所有文件都变成了绿色,暂存待提交状态 当我再次提交时,就出现错误:git fatal: cannot lock ref 'HEAD':unab ...

  5. undefined reference to 'floor'/undefined reference to

    undefined reference to 'floor'/undefined reference to `& undefined reference to 'pow'/undefined ...

  6. 问题排查--@cannot lock ref 'HEAD': unable to resolve reference 'refs/heads/master': reference broken解决方法

    @cannot lock ref 'HEAD': unable to resolve reference 'refs/heads/master': reference broken解决 作者用的是ID ...

  7. C++ Reference: Standard C++ Library reference: C Library: cmath: cbrt

    C++官网参考链接:https://cplusplus.com/reference/cmath/cbrt/ 函数  <cmath> <ctgmath> cbrt C99 dou ...

  8. C++ Reference: Standard C++ Library reference: C Library: cmath: erf

    C++官网参考链接:https://cplusplus.com/reference/cmath/erf/ 函数  <cmath> <ctgmath> erf C99 doubl ...

  9. C++ Reference: Standard C++ Library reference: C Library: cstdio: printf

    C++官网参考链接:https://cplusplus.com/reference/cstdio/printf/ 函数  <cstdio> printf int printf ( cons ...

最新文章

  1. typescript 叹号_TypeScript系列(五)最佳实践
  2. 都说 WebP 厉害,究竟厉害在哪里?
  3. react-router browserHistory刷新页面404问题解决
  4. redis在linux命令行下连续进行命令操作
  5. 综述ASP.NET下的AJAX模式
  6. entitymanager_实体管理器的类型:应用程序管理的EntityManager
  7. 什么是写一个java类,Java什么是类?class的相关介绍
  8. cv mat的shape_将ndarray转换为cv::Mat的最简单方法是什么?
  9. 怎么将短连接修改为长连接_回音壁怎么选?Redmi这个还不错
  10. K-L变换原理、图像压缩与人脸识别实战(附matlab代码)
  11. PyQt5-QLineEdit控件使用
  12. 容器技术Docker K8s 4 容器编排技术基础-Kubernetes
  13. Android TextToSpeech TTS中文文本转语音(语音合成)
  14. 电脑死机,Word忘了保存怎么恢复?(编辑器是WPS)
  15. 深度学习计算框架综述(一)行业内计算框架总览
  16. linux ubuntu设置中文,ubuntu 中文设置 (LANG设置)
  17. JS的数据类型分类以及用法,没有这些基础,学不好js
  18. Github优秀Android开源项目,值得引用与学习(图文结合~~~)
  19. 方向导数和梯度(grad)
  20. 不惧严寒的涩北“特种兵”

热门文章

  1. CS231n笔记-CNN网络结构
  2. WiFi遇到 无法连接到这个网络 的解决办法记录 ( netsh winsock reset )
  3. 01.JS基础_前端的语法(4)
  4. 程序员副业之如何利用空余时间从博客中赚钱?
  5. 【运筹学】对偶理论 : 总结 ( 对偶理论 | 原问题与对偶问题对应关系 | 对偶理论的相关结论 ) ★★★
  6. 微信公众号运营策划方案书之公众号基础知识
  7. eChat(微聊天)
  8. 招聘移动全栈工程师(iOS 方向)
  9. 如何清理windows server 2008 R2 中winsxs文件夹
  10. 光模块价格由带宽还是距离决定_广州单模光模块价格