程序报memory fault错误,请高手指点

hotsmile 2010-08-13 09:35:34
#include <string.h>
#include <stdio.h>
#include <stdlib.h>


/*
Name:
Copyright:
Author:
Date: 13-08-10 19:33
Description:
*/




void parse_xml (char * line){

/*根据=来解析*/

char row[] = "<row cola=\"\" colb=\"i222222\"/>";
char *p = NULL;
char *q = NULL;

p=strrstr(line," ");
puts(p);
q = ++p;
p= strrstr(line,"/");
*p = '\0';
puts(q);
p = q;
/*
*q => a="1" b="2"
*/

char col_name[128];
char col_value[1024];
int i=0;
while(p!=NULL){
/* extra col name */
p = strstr(p,"=");
if(p==NULL)break;
*p = '\0';
strcpy(col_name,q);

/*skip " */
p += 2;
q = p;

/* extra col value */
p = strstr(p,"\"");
*p++ = '\0';
strcpy(col_value,q);
i++;
printf("for number:%d",i);
printf("col name:%s,",col_name);
printf("col value:%s\n",col_value);

/*skip blank*/
while(*p==' '){
p++;
}
q = p;
}

}
int main(void)
{
int read=0;
FILE * fp;
char * line = NULL;
int len = 200;

fp = fopen("/tmp/a.xml", "r");
if (fp == NULL)
exit(0);
while ((fgets(line, len, fp)) != NULL) {

printf("%s", line);
/*把每一行转成结构体*/
parse_xml(line);


}

fclose(fp);
return 1;
}











...全文
829 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hotsmile 2010-08-16
  • 打赏
  • 举报
回复
问题已经解决,谢谢先
Jim_King_2000 2010-08-13
  • 打赏
  • 举报
回复
一般导致memory fault大都是因为buffer overrun。由于C/C++不检查越界行为,因此越界产生的错误往往在很后面才会出现,且出现错误时已经很难找到错误点。
对付buffer overrun一般有下面两个小手段:
1、用assert仔细检查每一个下标。
2、用memory debugger(purify, boundschecker, valgrind)检查程序。
jackyjkchen 2010-08-13
  • 打赏
  • 举报
回复
strrstr在VS08里找不到……

换成strstr没报错的说

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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