sprintf && sscanf

sprintf
作用:格式化字符串

char buf[128]; //或者动态分配内存 char* buf = (char*)malloc(128);
sprintf(buf, "name:%s, age:%d", "haocan_xu", 25); //运行结果为buf的内容是 name:haocan_xu, age:25

sscanf
作用:解析字符串(ps:sscanf只适用于提取数字,不能提取字符串)

const char* text = "2018-7-6";
int year, month, day;
int n = sscanf(text, "%d-%d-%d", &year, &month, &day); //n=3, sscanf的返回值为提取出的段落数量,例如char* text = "2018-7-",则n=2

猜你喜欢

转载自blog.csdn.net/Xu_Haocan/article/details/80937903
今日推荐