首页 文章

fclose上的分段错误

提问于
浏览
-1

我在Linux中运行程序时遇到Segmentation fault错误 . 在AIX中正常工作 .

我在gdb中得到了一条带有错误信息的段错误...

从/lib64/libc.so.6编程接收信号SIGSEGV,分段故障fclose @@ GLIBC_2.2.5()

<pre><code>
            #include<stdio.h>
            #include<string.h>
            #include<unistd.h>
            #include<time.h>
            #include<sys/types.h>
            #define _Monthly "99992016"


            FILE *source,*list,*wlist,*movlist,*biflis;
            char sf[85],sf1[85];
            char filename[85],filename1[85],name[140],fstring[140],iname[140],account[85];
            char *ptr="",*st="";
            int i,len,x,y,z,BIF_COUNT,PIN_COUNT,YOUR_MONTHLY_COUNT,count1,count2;
            char suffix[4]=".pin";
            char *nptr=" ";
            FILE *biflis;
             int p;
            int main()
            {
            long t;
            system("ls -l|sort -n >test.txt");
            system("cat test.txt|grep BH|awk '$5 <= 1610612736 {print $9}' > bhatia.out");
            system("cat test.txt|grep BH|awk '$5 > 1610612736 {print $9}' > bhatia1.out");     
            if((list=fopen("bhatia.out","r"))==NULL)
            {  printf("\n Error opening bhatia.out\n\n "); return 0; }

             if((biflis=fopen("biffile_account.lis","w"))==NULL)
             {   printf("\n Error opening biffile_account.lis file\n\n");
              return 0; }
            BIF_COUNT=0;
            PIN_COUNT=0;
            YOUR_MONTHLY_COUNT=0;
            count1=0;
            while((fgets(filename,85,list))!=NULL)
            {
                ptr=filename;
             for(i=0;*ptr;i++,ptr++)
             {
              sf[i]=*ptr;
             }i--;
             sf[i]='\0';

             if((source=fopen(sf,"r"))==NULL)
             {
              printf("\nError Opening Source File %s\n",sf);
              return 0;
             }
            fseek(source,0,SEEK_END);
            fseek(source,-90,SEEK_CUR);
             while((fgets(name,140,source))!=NULL)
             {
              st=strstr(name,_Monthly);
             count2=0;
              if(st!=NULL)
              {
            YOUR_MONTHLY_COUNT++;
            strcpy(account," ");
            count2++;
            //sprintf(fstring,"%s~%s",filename1,account);
            }
             count1= count2 + count1;
            }
            fclose(source);
            if( count1 > 0 )
            {
              printf(" %s   GBIF file  has the correct footer    having ",sf,source);
                printf(" %d number of correct bills \n",count1);
            } 
               else
            {
            printf(" %s   GBIF file has the footer missing and have  incorrect bills  \n",sf,source);
            //printf(" %d number of in incorrect bills \n",count1);
            }
                 count1=0;
            BIF_COUNT++;
            } 
            </code></pre>

1 回答

  • 3

    这是一个主要的错误:

    printf(" %s   GBIF file  has the correct footer    having ",sf,source);
    

    您还需要遍历此处声明的所有变量:

    FILE *source,*list,*wlist,*movlist,*biflis;
    

    他们中的每一个都需要一次调用fopen和一次调用fclose,否则他们应该被删除 . 你也两次宣布 FILE *biflis; .

    并且根本不需要使用全局变量 .

相关问题