使用sscanf检查空白长度

时间:2013-12-07 23:41:13

标签: c cstring scanf

如何使用sscanf验证此字符串“string int int”中每个参数string,int,int之间只有一个空格?

1 个答案:

答案 0 :(得分:0)

使用%n

// "string int int"
char dest[100];
int d[2];
int n[4];
int result = sscanf(buf, "%99s%n %n%d%n %n%d", 
  dest, &n[0], &n[1], &d[0], &n[2], &n[3], &d[1]);
if ((result == 3) && (n[1] - n[0] == 1) && (n[3] - n[2] == 1)) {
  LifeIsGood();
}
相关问题