格式化字符串
sprintf
用于组包,将零散的数据,按照固定的格式,组成字符串。一般用来发送信息
头文件:#include<stdio.h>
int sprintf(char *str, const char *format, ......)
返回值为实际组包的长度

sscanf
用于解包,解析特定格式的数据
头文件:#include<stdio.h>
int sscanf(const char *restrict str, const char *restrict format, ...)
%d 用于提取数值 '0' - '9'

%s 用于提取字符串,遇到 '\0' 、空格、回车就结束

%*d 跳过整数,%*s跳过字符串

%[width]s ,%[width]d 读取指定宽度的数据

%[a-z] 表示匹配 a 到 z 中任意字符(尽可能多的匹配)


%[aBc] 表示匹配a、B、c中任意一个

%[^aBc] 表示匹配非a、B、c的任意字符


浙公网安备 33010602011771号