我正在尝试从文件中获取每一行并进行一些字符串操作。但是我的一段代码抛出了段错误。我将同一行作为不同的程序测试了相同的东西,它工作正常。但是当我从文件中读取并执行字符串操作时,它会引发分段错误。
变量声明:
char *pch3,str1[100],str2[100]
pch3 = strtok(line3,",");
while(pch3!=NULL)
{
if(strcmp(pch3,"?") == 0)
{
strcat(str1,"0");
strcat(str1,",");
}
else
{
strcat(str1,pch3);
strcat(str1,",");
}
pch3 = strtok(NULL,",");
}
strlen1=strlen(str1);
memcpy(str2,str1,strlen1-1);
fp2=fopen("breast-cancer-wisconsin-miscellaneous-cleansed.data","a");
fprintf(fp2,"%s\n",str2);
fclose(fp2);