gpt4 book ai didi

c - fclose 上的段错误

转载 作者:行者123 更新时间:2023-11-30 18:37:13 24 4
gpt4 key购买 nike

我在 Linux 中运行程序时遇到段错误错误。在 AIX 中工作正常。

我在 gdb 中收到消息段错误...

程序接收信号SIGSEGV,段错误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 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>

最佳答案

这是一个主要错误:

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

您还需要检查此处声明的所有变量:

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

其中每一个都需要调用一次 fopen 和调用一次 fclose,否则应将其删除。另外,您还声明 FILE *biflis; 两次。

而且根本不需要使用全局变量。

关于c - fclose 上的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38185399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com