求助! 段错误,不知道错在哪,求高手指导
编译提示段错误 找了半天没找出错误在哪 要求是将一个TXT文件以CLASS:为分类进行分类,归档到不同的TXT文件中去,去掉不需要的内容.
我把需要归档的原TXT文档和我写的代码都打包,请各位高手不吝赐教!! 谢谢
归档.rar
(28.11 KB)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *strstrip(char *s) /* 功能是去掉字符串中的空格*/
{
size_t size;
char *end;
if(!size)
return s;
end = s + size -1;
while( end>=s && isspace(*end))
end--;
*(end+1)='\0';
while(*s && isspace(*s))
s++;
return s;
}
int main(int argc,char * argv[])
{
FILE *fp = fopen(argv[1],"r"); /*传参文件名*/
FILE *fpecid = fopen("ECID.txt","w+"); /*打开CLASS: ECID归档目标文件*/
FILE *fpceid = fopen("CEID.txt","w+"); /*打开CLASS: CEID归档目标文件*/
FILE *fpsvid = fopen("SVID.txt","w+"); /*打开CLASS: SVID归档目标文件*/
FILE *fpalid = fopen("ALID.txt","w+"); /*打开CLASS: ALID归档目标文件*/
FILE *fpdvval = fopen("DVVAL.txt","w+"); /*打开CLASS: DVVAL归档目标文件*/
FILE *fplimit = fopen("LIMIT.txt","w+"); /*打开CLASS: LIMIT归档目标文件*/
if(fp<0){ /*判断是否打开传参文件成功*/
perror("fopen err");
return -1;
}
int flag = -1; /*设置一个标志,将不是CLASS 开头的字符串输入到相应的内容下面去*/
char buf[1000] = {0};
char * tmp = NULL;
while(tmp = fgets(buf,1000,fp)){
if( strcmp(buf,"*") == 0 || strcmp(buf,"#")==0){ /*将包涵"*" 和 "#"的内容去掉*/
continue;}
else if(strstr(buf,"CLASS:")) { /*找到关键词"CLASS:"开头的这一行*/
char *tmp_buf = malloc(strlen(buf)+1); /*申请内存空间*/
char *bak_buf = tmp_buf; /*为释放空间做预留*/
memset(tmp_buf,0,(strlen(buf))); /*清空*/
memcpy(tmp_buf,buf,strlen(buf)); /*复制*/
strsep (&tmp_buf,":"); /*将 ":" 之后的内容复制到tmp_buf中去*/
char *type = strstrip(tmp_buf); /*去掉 tmp_buf中字符串的空格*/
if(strcmp(type,"ECID")==0) /*做类型比较,将这一行内容归类到相应文档中去*/
flag = 0;
fputs(buf,fpecid);
if(strcmp(type,"SVID")==0)
flag = 1;
fputs(buf,fpsvid);
if(strcmp(type,"CEID")==0)
flag = 2;
fputs(buf,fpceid);
if(strcmp(type,"ALID")==0)
flag = 3;
fputs(buf,fpalid);
if(strcmp(type,"DVVAL")==0)
flag = 4;
fputs(buf,fpdvval);
if(strcmp(type,"LIMIT")==0)
flag = 5;
fputs(buf,fplimit);
free(bak_buf); /*释放空间*/
} else { /*将不是以CLASS开头的内容复制到相对应的内容下面去*/
switch(flag) {
case 0: fputs(buf,fpecid);break;
case 1: fputs(buf,fpsvid);break;
case 2: fputs(buf,fpceid);break;
case 3: fputs(buf,fpalid);break;
case 4: fputs(buf,fpdvval);break;
case 5: fputs(buf,fplimit);break;
default: printf("找不到可归类文档!\n");break;
}
}
}
fclose(fp);
fclose(fpecid);
fclose(fpsvid);
fclose(fpceid);
fclose(fpalid);
fclose(fpdvval);
fclose(fplimit);
return 0;
}
[ 本帖最后由 s58417632 于 2010-5-30 13:22 编辑 ]