一道关于字符串的问题

tanwei1002 2009-08-20 04:28:10
如下代码 我想将字符串中seqID,mobile后面的数据保存在已经定义好的结构中

用下面代码没有实现,但是我又找不出原因在哪?那位高手指点下 谢谢 了


typedef struct Test
{
char seqID[10];
char mobile[10];
}TEST;

//////////////////////////////
TEST test;

char bufRecv[4096] = "return=0;count=7;seqID=1328311111111653,mobile=1358438820011-16,Content";
char bufReceipt[512];
LPCTSTR pSeqID,pMobile,pContent,pTime;
if (pSeqID = strstr(bufRecv, "seqID="))
{
pSeqID += strlen("seqID=");
if (pMobile = strstr(pSeqID, ",mobile="))
{
if (pContent = strstr(pMobile, ",Content"))
{
pMobile += strlen(",mobile=");
int nSeqIDLen = pMobile-pSeqID -strlen(",mobile=");
nSeqIDLen = nSeqIDLen>10?10:nSeqIDLen; //由于结构体接收的空间为10
//所以数据过长则截取前10个字符
memcpy(test.seqID, pSeqID, nSeqIDLen);
test.seqID[nSeqIDLen] = 0;

int nMobileLen = pContent-pMobile-strlen(",content=");
nMobileLen = nMobileLen>10?10:nMobileLen;
memcpy(test.mobile, pMobile, nMobileLen);
test.mobile[nMobileLen] = 0;
}
}
}
...全文
86 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
tanwei1002 2009-08-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 yuhudie203 的回复:]
test.seqID[nSeqIDLen] = 0;
[/Quote]

恩 的确是这
数组越界了 谢谢了 已经解决了。
huhaoconga 2009-08-20
  • 打赏
  • 举报
回复
跟踪调试
yuhudie203 2009-08-20
  • 打赏
  • 举报
回复
test.seqID[nSeqIDLen] = 0;
fishion 2009-08-20
  • 打赏
  • 举报
回复
char bufRecv[4096] = "return=0;count=7;seqID=1328311111111653,mobile=1358438820011-16,Content";
char bufReceipt[512];
TEST test;
memset(&test, 0x00, sizeof(test));
memset(&bufReceipt, 0x00, sizeof(bufReceipt));
LPCSTR pSeqID,pMobile,pContent,pTime;
if (pSeqID = strstr(bufRecv, "seqID="))
{
pSeqID += strlen("seqID=");
if (pMobile = strstr(pSeqID, ",mobile="))
{
if (pContent = strstr(pMobile, ",Content"))
{
pMobile += strlen(",mobile=");
int nSeqIDLen = pMobile-pSeqID -strlen(",mobile=");
nSeqIDLen = nSeqIDLen>10?10:nSeqIDLen; //由于结构体接收的空间为10
//所以数据过长则截取前10个字符

memcpy(test.seqID, pSeqID, nSeqIDLen);
test.seqID[nSeqIDLen] = 0;
printf("%s",test.seqID);
int nMobileLen = pContent-pMobile;
nMobileLen = nMobileLen>10?10:nMobileLen;
memcpy(test.mobile, pMobile, nMobileLen);
test.mobile[nMobileLen] = 0;
}
}
}

没问题
tanwei1002 2009-08-20
  • 打赏
  • 举报
回复
是越界了 谢谢大家
yuhudie203 2009-08-20
  • 打赏
  • 举报
回复
报的错误贴下吧!
见习学术士 2009-08-20
  • 打赏
  • 举报
回复

//在memcpy(, pMobile, nMobileLen);之前初始化一下
memset(&test, 0x00, sizeof(test));
fishion 2009-08-20
  • 打赏
  • 举报
回复
明白了
fishion 2009-08-20
  • 打赏
  • 举报
回复
pSeqID += strlen("seqID=");??
tanwei1002 2009-08-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 coderofvc 的回复:]
自己调试一下就知道问题出在哪了。。
[/Quote]

调试出在这
 int nMobileLen = pContent-pMobile;
nMobileLen = nMobileLen>10?10:nMobileLen;
memcpy(test.mobile, pMobile, nMobileLen);///////问题在这 但是这儿复制怎么会影响test.seqID的值呢?
test.mobile[nMobileLen] = 0;
CoderOfVC 2009-08-20
  • 打赏
  • 举报
回复
自己调试一下就知道问题出在哪了。。
tanwei1002 2009-08-20
  • 打赏
  • 举报
回复
贴错了点点 代码如下
但是经过运行之后的test.seqID有问题。。。。

typedef struct Test
{
char seqID[10];
char mobile[10];
}TEST;

//////////////////////////////
TEST test;

char bufRecv[4096] = "return=0;count=7;seqID=1328311111111653,mobile=1358438820011-16,Content";
char bufReceipt[512];
LPCTSTR pSeqID,pMobile,pContent,pTime;
if (pSeqID = strstr(bufRecv, "seqID="))
{
pSeqID += strlen("seqID=");
if (pMobile = strstr(pSeqID, ",mobile="))
{
if (pContent = strstr(pMobile, ",Content"))
{
pMobile += strlen(",mobile=");
int nSeqIDLen = pMobile-pSeqID -strlen(",mobile=");
nSeqIDLen = nSeqIDLen>10?10:nSeqIDLen; //由于结构体接收的空间为10
//所以数据过长则截取前10个字符
memcpy(test.seqID, pSeqID, nSeqIDLen);
test.seqID[nSeqIDLen] = 0;

int nMobileLen = pContent-pMobile;
nMobileLen = nMobileLen>10?10:nMobileLen;
memcpy(test.mobile, pMobile, nMobileLen);
test.mobile[nMobileLen] = 0;
}
}
}

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧