这次栽倒在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;
}
结果很正常:
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;
}
结果为:
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;
}
结果为:
nRet is 2
yes, key
yes, value
以后还是要小心啊, 定位较长时间, 才发现是栽倒在这个最简单的地方
。
转载自
http://blog.csdn.net/stpeace/article/details/45725517 我也遇到这种极品问题,目前还在想办法解决。
[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 我也遇到这种极品问题,目前还在想办法解决。
相关文章推荐
- SOAPUI笔记2:SoapUI widow破解
- queue_01
- executeScalar ExecuteNonQuery
- jdgui反编译+javac编译=无源文件改动代码
- unique()去重函数
- iOS 动画Animation - 5:UIBezier
- ZOJ 2136 Longest Ordered Subsequence
- UVA 11039 Building Designing
- ZOJ 3210 A Stack or A Queue?【模拟】
- vuejs心法和技法
- Android Builder模式
- 深入研究String StringBuffer StringBuilder 的拼接字符串
- for,break和continue
- deque_01
- poj 2031 Building a Space Station
- FZU 1914 Funny Positive Sequence
- UI自动布局
- FZU 1914 Funny Positive Sequence
- GUI实现显示当前时间
- GUI实现万年历