报错为:Segmentation fault(coredump)

int main(int argc, char *argv[]) 


char name [120];
char ch[MAX_LINE_SIZE];
char str[MAX_LINE_SIZE];
char * pf,* pt;
int charge;
int sum;
int i,j;
FILE * fp;
sprintf(name,"%s",argv[1]); 
printf("name is [%s]\n",name);
memset(str,0,MAX_LINE_SIZE);
memset(name,0,sizeof(name));
fp=fopen(name,"r");
while(1)

printf("begin\n");
memset(ch,0,MAX_LINE_SIZE);
if(fgets(ch,MAX_LINE_SIZE,fp) == NULL)
{
break;
}

Helenr
浏览 191回答 1
1回答

慕雪6442864

memset(name,0,sizeof(name));fp=fopen(name,"r");文件名被memset了,估计没有打开成功,然后fgets就core了。需要判断fp是否为NULL,再进行文件操作
打开App,查看更多内容
随时随地看视频慕课网APP