C/C++——C语言格式化输入输出

声明:本文系原创,转载请注明出处。

 

C语言中的几种格式化输入/输出方式:

1. 函数功能:

  •  scanf();  //从标准输入设备(控制台键盘)输入 
  •  sscanf(); //从字符串缓冲区输入与指定格式相符的数据 
  •  fscanf(); //从文件输入 
  •  printf(); // 从标准输出设备(显示器)输出 
  •  sprintf(); //向字符串缓冲区输出与指定格式相符的数据 
  •  fprintf(); //向文件输出 

 

2. 函数原型:

复制代码

//格式化输入函数
int scanf ( const char * format, ... );
int sscanf ( const char * s, const char * format, ...);
int fscanf ( FILE * stream, const char * format, ... );

//格式化输出函数
int printf ( const char * format, ... );
int sprintf ( char * str, const char * format, ... );
int fprintf ( FILE * stream, const char * format, ... );

复制代码

 

3. 函数参数:

以上各函数无非是 从/向 缓冲区/文件(包括标准输入/输出)对数据进行格式化输入/输出。

关键参数是 const char * format , format 是一个字符串字面常量,其中含有若干格式描述符(format specifier),输入/输出函数的作用就是把给定的数据按照格式描述符(format specifier)规定的格式进行输入/输出。

一个格式描述符(format specifier)具有如下范式:

%[flags][width][.precision][length]specifier 

下面以 printf 为例进行说明:

1)specifier 

其中必不可少的是 specifier ,因为它规定了数据的类型和对其进行的解释, specifier 的取值及含义如下:

specifierOutputExample
or i有符号十进制整数392
u无符号十进制整数7235
o无符号八进制整数610
x无符号十六进制整数7fa
X无符号十六进制整数(大写形式)7FA
f十进制浮点数,小写形式392.65
F十进制浮点数,大写形式392.65
e科学记数法,小写形式3.9265e+2
E科学记数法,大写形式3.9265E+2
g%e 和 %f 中的最短表示式392.65
G%E 和 %F 中的最短表示式392.65
a十六进制浮点数,小写形式-0xc.90fep-2
A十六进制浮点数,大写形式-0XC.90FEP-2
c单字符a
s字符串sample
p        指针的地址b8000000
n

不输出任何东西。

与之对应的参数必须是一个指向有符号整型变量的指针;

函数执行的结果是将至今已经写入的字符数目写入该指针指向的内存中。

 
%%%将会输出一个%%

  specifier 可选的修饰描述符 sub-specifiers 包括: flags 、 width 、 .precision 等。

 

2)sub-specifiers 

flagsdescription
-根据给定的宽度左对齐。默认情况下是右对齐。
+对结果强制附加正号+或符号-在前面,即是是正数也要前置+。默认情况下是只有负数前置-。
(space)如果没有任何符号位说明,将会在数据前插入一个空格。
#

当描述符是o, x 或 X 时,若值非0,则结果前置0, 0x 或 0X;

当描述符是a, A, e, E, f, F, g 或 G时,若值非0,强制添加十进制小数点至输出,即使小数点位后没有其他数字。默认情况下,若小数点位置后无数字,则不予输出小数点。

0     当可填充时,用数字0而不是默认的空格填充剩余的空间。

 

widthdescription
(number)

最少打印number个字符。

如果待打印数据的宽度比number小,则结果会以空格进行填充。

如果结果的宽度大于number,则按实际宽度打印,而不进行截断。

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

 

.precisiondescription
.numberFor 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.
.*        The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

 

3)modifiers

length参数可以修改数据的类型长度。

 specifiers
lengthd iu o x Xf F e E g G a Acspn
(none)intunsigned intdoubleintchar*void*int*
hhsigned charunsigned char    signed char*
h      short intunsigned short int    short int*
llong intunsigned long int wint_twchar_t* long int*
lllong long intunsigned long long int    long long int*
jintmax_tuintmax_t    intmax_t*
zsize_tsize_t    size_t*
tptrdiff_tptrdiff_t    ptrdiff_t*
L  long double    

说明:hh, ll 同h, l, 但仅对C99有效。

 

4. 函数返回值

当读写成功时,返回被读写的字符数;

当读写失败时,返回一个负数。

 

5. sscanf/scanf正则用法

  • scanf族函数会忽略一行开始的空白,直到第一个非空白符出现为止,空白符有:
    • space: ' '
    • tab: '\t'
    • newline: '\n'
  • %* :表示空读一个数据
    • %*d:匹配到的整数被过滤掉;
    • %*s:匹配到的字符串被过滤掉;
    • %*[a] :匹配到的a字符均被过滤掉;
    • %*[分隔符]:经常读取的字符串中有分隔符,可以将分隔符过滤掉。
  • %[ ] 的用法:
  • %[ ] 表示要读入一个字符集合 , 如果 [ 后面第一个字符是 ”^” ,则表示反意思。
  • [ ] 内的字符串可以是 1 或更多字符组成。
  • 空字符集( %[] )是违反规定的,可导致不可预知的结果。 %[^] 也是违反规定的。
  • %[] 有很大的功能,但是并不是很常用到,主要因为:
    1. 许多系统的 scanf 函数都有漏洞. (典型的就是 TC 在输入浮点型时有时会出错);
    2. 用法复杂 , 容易出错;
    3. 编译器作语法分析时会很困难 , 从而影响目标代码的质量和执行效率.

  • %[a-z] 读取在 a-z 之间的字符串,如果不在此之间则停止,如:
    • char s[]="hello, my friend” ; // 注意 : , 逗号在不 a-z 之间
      sscanf( s, “%[a-z]”, string ) ; // string=hello
  • %[^a-z] 读取不在 a-z 之间的字符串,如果碰到 a-z 之间的字符则停止,如:
    • char s[]="HELLOkitty” ;// 注意 : , 逗号在不 a-z 之间
      sscanf( s, “%[^a-z]”, string ) ; // string=HELLO
  • %*[^=] 前面带 * 号表示不保存变量。跳过符合条件的字符串。
    • char s[]="notepad=1.0.0.1001" ;
      char szfilename[32] = "" ;
      int i = sscanf( s, "%*[^=]", szfilename ) ; // szfilename=NULL, 因为没保存
      int i = sscanf( s, "%*[^=]=%s", szfilename ) ; // szfilename=1.0.0.1001
  • %40c 读取 40 个字符
  • %[^=] 读取字符串直到碰到 '=' 号, '^' 后面可以带更多字符 , 如:
    • char s[]="notepad=1.0.0.1001" ;
      char szname[32] = "" ;
      char szver[32] = “” ;
      int i = sscanf( s, "%[^=]", szname ) ; // szname=notepad
      sscanf( s, "%[^=]=%s", szname , szver ) ; // szname=notepad, szver=1.0.0.1001

 

 


参考资料

  1. http://www.cplusplus.com/reference/cstdio/scanf/
  2. http://www.cplusplus.com/reference/cstdio/printf/
  3. https://blog.csdn.net/a_Treasure/article/details/82085937
  4. https://blog.csdn.net/21aspnet/article/details/174326
  5. https://www.cnblogs.com/mylinux/p/4635510.html
  6. https://www.jb51.net/article/138506.htm

原文链接:http://www.cnblogs.com/oddcat/articles/9773312.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值