znglyfe 2016-11-07 06:55 采纳率: 33.3%
浏览 1049
已采纳

linux中关于fgets函数段错错

root@ubuntu:/my# ./fun jpeglib.h jpeg.h
44444
Segmentation fault (core dumped)
下面是源码
root@ubuntu:/my# cat 2.c
#include

int main(int argc, char **argv)
{
FILE *fp1, *fp2;
char buff[124];
char buff1[124];
char *temp;
int i = 0;
if((fp1 = fopen(argv[1], "r")) == NULL){
printf("can not open file %s\n", argv[1]);
return -1;

}

if((fp1 = fopen(argv[2], "wa+")) == NULL){
            printf("can not open file %s\n", argv[2]);
                  return -1;

 }
printf("44444\n");
 while(fgets(buff, 124, fp1) != NULL){
    printf("-------------\n");
    temp = buff;
    printf("++\n");
    while(*temp++ != '0'){
        buff1[i] = buff[i+1];
        i++;
        printf("%d\n", i);
    }
    fputs(buff1, fp2);   
 }

 fclose(fp1);
 fclose(fp2);
 return 0;

}

  • 写回答

2条回答 默认 最新

  • 云镛 2016-11-07 09:11
    关注

    你的fp2为空啊,没有打开啊。你fp1打开了两次。
    还有啊,干嘛这么麻烦呢?参考下面的代码:
    while(fgets(buff, 124, fp1) != NULL){
    fputs(buff1, fp2);

    }
    这样多好,你是想把.h文件内容复制到第二个.h中吧?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?