您的位置:首页 > 产品设计 > UI/UE

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

2016-04-19 21:50 513 查看
之前用sscanf也是得心应手的, 比如:

[cpp]
view plain
copy

print?

#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;  
}  

#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

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

[cpp]
view plain
copy

print?

#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;  
}  

#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仍然是空串。 那该怎么改呢?如下:

[cpp]
view plain
copy

print?

#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;  
}  

#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.csdn.net/stpeace/article/details/45725517  我也遇到这种极品问题,目前还在想办法解决。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: