2016-07-04 16 views
-1

Ich bekomme Segmentierung Fehler beim Ausführen von Programm in Linux. Funktioniert gut in AIX.Segmentation Fault on fclose

Und ich seg Fehler mit der Meldung in gdb bekommen ...

Programm empfangene Signal SIGSEGV, Fehler Segmentation fclose @@ GLIBC_2.2.5() von /lib64/libc.so.6

  <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

Bitte minimieren Sie den problematischen Testfall und formatieren Sie ihn in etwas lesbares. – domen

Antwort

3

hier ist eine wichtige Bug:

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

Sie müssen auch hier erklärt alle Variablen durchlaufen:

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

Jede einzelne von ihnen muss einen Aufruf zum fopen und einen Aufruf zum Schließen haben, oder sie sollten entfernt werden. Außerdem deklarieren Sie zweimal FILE *biflis;.

Und es ist nicht notwendig, globale Variablen überhaupt zu verwenden.

+0

Wie kann ich diesen Fehler entfernen .. printf ("% s GBIF-Datei hat die richtige Fußzeile mit", sf, source); –

+2

@MikeSidhu Indem Sie ein Minimum an Aufwand in Ihrem Namen aktivieren und diese Zeile zweimal lesen. Macht das Sinn? Tut es, was Sie vorhatten? – Lundin

+0

Danke .. ich habe es .. –