c - Segmentation Fault on fclose -
i getting segmentation fault error while running program in linux. working fine in aix.
and seg fault message in gdb...
program received signal sigsegv, segmentation fault fclose@@glibc_2.2.5 () /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 correct footer having ",sf,source); printf(" %d number of correct bills \n",count1); } else { printf(" %s gbif file has footer missing , have incorrect bills \n",sf,source); //printf(" %d number of in incorrect bills \n",count1); } count1=0; bif_count++; } </code></pre>
here's 1 major bug:
printf(" %s gbif file has correct footer having ",sf,source);
you need go through variables declared here:
file *source,*list,*wlist,*movlist,*biflis;
every single 1 of them needs have 1 call fopen , 1 call fclose, or should removed. declare file *biflis;
twice.
and there no need use global variables @ all.
Comments
Post a Comment