swscanf_s和sscanf_s的使用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swscanf_s和sscanf_s的使用相关的知识,希望对你有一定的参考价值。
//(1) TCHAR buf[12] = { 0 }; swscanf_s(_T("a/n"), _T("%*[^/]/%s"), buf,12); TRACE(_T("%s"), buf); //(2) TCHAR buf3[10] = { 0 }; TCHAR buf4[10] = { 0 }; swscanf_s(_T("a=b"), _T("%[^=] %*[=]%s"), buf3, 10,buf4,10); TRACE(_T("%s %s \n"), buf3,buf4); //(3) int a, b, c; swscanf_s(_T("2018-12-29"), _T("%d-%d-%d"), &a, &b, &c); TRACE(_T("%d-%d-%d"), a, b, c); //(4) TCHAR buf1[12] = { 0 }; TCHAR buf2[12] = { 0 }; swscanf_s(_T("anAB"), _T("%[a-z]%[A-Z]"), buf1, 12,buf2,12); TRACE(_T("%s ,%s"), buf1,buf2); //(5) int ip[4] = { 0 }; int ret = sscanf_s(" 192. 168.1. 213", "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]); if(ret != 4)TRACE(_T("ip error")); if (ip[0] > 255 || ip[0] <= 0 || ip[1]>255 || ip[1] <= 0 || ip[2]>255 || ip[2] <= 0 || ip[3]>255 || ip[3] <= 0) { TRACE(_T("ip error")); } else { TRACE(_T("ip ok")); }邮件地址判断,如下:
TCHAR buf1[12] = { 0 }; TCHAR buf2[12] = { 0 }; TCHAR buf3[12] = { 0 }; int ret = swscanf_s(_T("[email protected]"), _T("%[a-zA-B0-9] %*[@] %[a-zA-Z]%*[.]%s"), buf1, 12,buf2,12,buf3,12); TRACE(_T("ret=%d: %s %s %s\n"),ret, buf1,buf2,buf3); if (ret == 3) TRACE(_T("正确的邮件地址")); else TRACE(_T("不正确的邮件地址"));
以上是关于swscanf_s和sscanf_s的使用的主要内容,如果未能解决你的问题,请参考以下文章