sscanf()和sprintf()——按固定格式提取字符串、按固定格式生成字符串
sscanf()函数用于按照某种固定格式提取出一类字符串的变量,就像printf()是按照自定义的格式输出,sscanf()函数就是printf()函数反过来。
·
sscanf()函数
- sscanf()函数用于按照某种固定格式提取出一类字符串的变量,就像printf()是按照自定义的格式输出,sscanf()函数就是printf()函数反过来
int year = 0, month = 0, day = 0, hour = 0;
sscanf("ABCD_20230522080000_OSIDJO", "ABCD_%04d%02d%02d%02d0000_OSIDJO", &year, &month, &day, &hour);
cout<< year << month << day << hour <<endl;
结果:20235228
sprintf()函数
- sprintf()按照某种固定格式生成字符串
int year = 2068, month = 5, day = 6, hour = 14, minute = 43, second = 5;
char buff[256] = { 0 };
sprintf(buff, "CHINA_%04d%02d%02d_%02d%02d_WORD", year, month, day, hour, minute);
cout << buff << endl;
结果:CHINA_20680506_1443_WORD
更多推荐
所有评论(0)