这次栽倒在sscanf函数上------ 看看错误的语句:int nRet = sscanf("xxx=yyy", "%s=%s", szKey, szValue);

本文详细探讨了sscanf函数在不同场景下的使用方法及注意事项,通过实例展示了如何正确解析包含键值对的字符串,并进行了错误排查。重点强调了在字符串中使用变量作为分割符的重要性。
之前用sscanf也是得心应手的, 比如:

  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. int main()  
  5. {  
  6.     char szLine[100] = {0};  
  7.     int left = 0;  
  8.     int right = 0;  
  9.   
  10.     strncpy(szLine, "123=456"sizeof(szLine) - 1);  
  11.     int nRet = sscanf(szLine, "%d=%d", &left, &right);  
  12.     printf("nRet is %d\n", nRet);  
  13.       
  14.     printf("left is %d, right is %d\n", left, right);  
  15.   
  16.     return 0;  
  17. }  
#include <stdio.h>
#include <string.h>

int main()
{
	char szLine[100] = {0};
	int left = 0;
	int right = 0;

	strncpy(szLine, "123=456", sizeof(szLine) - 1);
	int nRet = sscanf(szLine, "%d=%d", &left, &right);
	printf("nRet is %d\n", nRet);
	
	printf("left is %d, right is %d\n", left, right);

	return 0;
}

      结果很正常:

nRet is 2
left is 123, right is 456


      现在, 碰到了字符串, 所以我随心所欲地类比写成:

  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. int main()  
  5. {  
  6.     char szLine[100] = {0};  
  7.     char szKey[50] = {0};  
  8.     char szValue[50] = {0};  
  9.   
  10.     strncpy(szLine, "xxx=yyy"sizeof(szLine) - 1);  
  11.     int nRet = sscanf(szLine, "%s=%s", szKey, szValue);  
  12.     printf("nRet is %d\n", nRet);  
  13.       
  14.     if(0 == strcmp(szKey, "xxx"))  
  15.     {  
  16.         printf("yes, key\n");  
  17.     }  
  18.   
  19.     if(0 == strcmp(szValue, "yyy"))  
  20.     {  
  21.         printf("yes, value\n");  
  22.     }  
  23.   
  24.     return 0;  
  25. }  
#include <stdio.h>
#include <string.h>

int main()
{
	char szLine[100] = {0};
	char szKey[50] = {0};
	char szValue[50] = {0};

	strncpy(szLine, "xxx=yyy", sizeof(szLine) - 1);
	int nRet = sscanf(szLine, "%s=%s", szKey, szValue);
	printf("nRet is %d\n", nRet);
	
	if(0 == strcmp(szKey, "xxx"))
	{
		printf("yes, key\n");
	}

	if(0 == strcmp(szValue, "yyy"))
	{
		printf("yes, value\n");
	}

	return 0;
}

       结果为:

nRet is 1


       从结果看, 解析失败, 为什么呢? 原来此时=并没有做分割符, 而是做了szKey的一部分, 此时szValue仍然是空串。 那该怎么改呢?如下:

  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. int main()  
  5. {  
  6.     char szLine[100] = {0};  
  7.     char szKey[50] = {0};  
  8.     char szValue[50] = {0};  
  9.   
  10.     strncpy(szLine, "xxx=yyy"sizeof(szLine) - 1);  
  11.     int nRet = sscanf(szLine, "%[^=]=%[^=]", szKey, szValue);  
  12.     printf("nRet is %d\n", nRet);  
  13.       
  14.     if(0 == strcmp(szKey, "xxx"))  
  15.     {  
  16.         printf("yes, key\n");  
  17.     }  
  18.   
  19.     if(0 == strcmp(szValue, "yyy"))  
  20.     {  
  21.         printf("yes, value\n");  
  22.     }  
  23.   
  24.     return 0;  
  25. }  
#include <stdio.h>
#include <string.h>

int main()
{
	char szLine[100] = {0};
	char szKey[50] = {0};
	char szValue[50] = {0};

	strncpy(szLine, "xxx=yyy", sizeof(szLine) - 1);
	int nRet = sscanf(szLine, "%[^=]=%[^=]", szKey, szValue);
	printf("nRet is %d\n", nRet);
	
	if(0 == strcmp(szKey, "xxx"))
	{
		printf("yes, key\n");
	}

	if(0 == strcmp(szValue, "yyy"))
	{
		printf("yes, value\n");
	}

	return 0;
}

        结果为:

nRet is 2
yes, key
yes, value


       以后还是要小心啊, 定位较长时间, 才发现是栽倒在这个最简单的地方微笑


转载自 

http://blog.youkuaiyun.com/stpeace/article/details/45725517  我也遇到这种极品问题,目前还在想办法解决。

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值