帮助需要关于在文件指针分割故障 [英] Help needed regarding the segmentation fault at the file pointer

查看:78
本文介绍了帮助需要关于在文件指针分割故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经已经遇到一个奇怪的问题,一个'C'程序..我得到段错误含的feof(FP)行了..
我试图在Linux上运行。

我甚至用gdb命令回溯程序..
但它是没有用的。

检查我的样本code ..

 字符BUF [2000],海峡[15],LNO [5],DEF [15],文献[15],TMP [15],CH,IFILE [20] OFILE [20];INT I,J,OLDI,计数,C,R,D,F,T,LC = 0;FILE * FP = NULL,* FPO = NULL;无效xyzstart()
{
/ *
*某些操作未在所有有关文件
*
* /
}诠释的main(){的printf(请输入输入文件\\ n的名称);得到(IFILE);FP = FOPEN(IFILE,R);
如果(FP == NULL){的printf(错误);出口(0);}的printf(请输入输出文件\\ n的名称);
得到(OFILE);FPO = FOPEN(OFILE,W);
如果(FPO == NULL){的printf(输出文件无法打开\\ n);出口(0);}
而(!的feof(FP)){与fgets(BUF,sizeof的(BUF),FP);算上++; //计算一个文件的行数}倒带(FP); //文件指针移动到文件的开头而(!的feof(FP))//错误就在这里!分段错误(核心转储)!{明确(); //用户定义的函数,它会清除所有内存如果(计数== LC){nodef(); //用户定义的功能,它不从读取或写入到一个文件中NOREF(); //用户定义的功能,它不从读取或写入到一个文件中打印(); //用户定义的函数,它的值写入到输出文件打破;}与fgets(BUF,sizeof的(BUF),FP);{I = 0;LC ++;
    而(BUF [I]!='')//读取直到遇到一个空间..      {      LNO [I] = BUF [I]      我++;      }    LNO [I] ='\\ 0';//看跌期权(LNO); ///    }我++;OLDI = I;CH = BUF [I]开关(CH){情况下'X':xyzstart();打破;默认:输出(没有找到);
}}FCLOSE(FPO);FCLOSE(FP);返回0;}

我真的不知道该怎么办!任何人都可以请帮我?
在此先感谢!

这里的code为既清晰又xyzstart()
    无效清晰()

  {memset的(BUF,'\\ 0',sizeof的(BUF));memset的(LNO,'\\ 0',sizeof的(LNO));memset的(DEF,'\\ 0',sizeof的(DEF));memset的(参考,'\\ 0',sizeof的(REF));I = OLDI = 0;memset的(STR,'\\ 0',sizeof的(STR));}无效xyzstart(){R = D = C = 0;为(;; C ++,我++)       {           如果(BUF [I] =='')           打破;           如果(BUF [I] =='(')破;           如果(BUF [I] ==';')破;           如果(BUF [I] =='\\ n')打破;           如果(BUF [I] =='=')破;           如果(BUF [I] =='+'|| BUF [I] ==' - ')破;           海峡[C] = BUF [I]        }        海峡[C] ='\\ 0';如果(BUF [I] =='=')           分配();否则,如果(BUF [I] =='+'|| BUF [I] ==' - ')//递增或递减            incdec();          否则如果(BUF [I] =='(')                udefined();}


解决方案

一些注意事项:


  1. 不要使用的feof(FP)作为while循环的条件;你试图读到文件结束后,该功能将只返回true,所以你会风循环一次,过于频繁。检查您输入操作的结果(与fgets 将返回失败NULL)和然后的测试EOF,就像这样:

     
    而(与fgets(buf中,sizeof的BUF,FP)!= NULL)
      算上++;
    如果(的feof(FP))
      的printf(在档案\\ n结束);


  2. 永远永远永远永远永远使用获得:它的在引入一个故障点的程序(可能在这种情况下)。它一直去pcated为C99的$ P $,预计从语言的下一个版本(是的,由此引起的一个库调用的混乱比打破30多年传统$ C $的前景可怕了C)。使用与fgets 或代替其他一些选择。


从您发布的code,我看不出有任何的明显的问题;我不知道为什么的feof 将核心转储如果previous声明是一个成功的退。我可以计算的唯一一件事是,文件指针被覆盖的地方(可能通过在获得调用中的缓冲区溢出)。

I've a 'C' program which has encountered a strange problem.. I'm getting segmentation fault in the line containing "feof(fp)".. I am trying to run on linux..

I even used gdb command to backtrace the program.. But it was of no use..

Check my sample code..

char buf[2000],str[15],lno[5],def[15],ref[15],tmp[15],ch,ifile[20],ofile[20];

int i,j,oldi,count,c,r,d,f,t,lc=0;



FILE *fp=NULL,*fpo=NULL;

void xyzstart()
{
/*
*Some operation that is not at all concerned with the file
*
*/
}

int main()

{

printf("Enter the name of the input file\n");

gets(ifile);



fp=fopen(ifile,"r");
if(fp==NULL)

{

printf("Error");

exit(0);

}



printf("Enter the name of the output file\n");
gets(ofile);

fpo=fopen(ofile,"w");


if(fpo==NULL)

{

printf("Output file couldn't be opened\n");

exit(0);

}




while(!feof(fp))

{

fgets(buf,sizeof(buf),fp);

count++;  //Count the number of lines in a file

}



rewind(fp); //move the file pointer to the beginning of the file



while(!feof(fp)) //Error is here!! Segmentation fault (Core Dumped)!!

{

clear();  //User defined function which clears all the memory

if(count==lc)

{

nodef();  //User defined function which doesn't reads from or writes into a file

noref();  //User defined function which doesn't reads from or writes into a file

print();  //User defined function which writes the values to output file

break;

}



fgets(buf,sizeof(buf),fp); 

{

i=0;

lc++;


    while(buf[i]!=' ') //read until it encounters a space..

      {

      lno[i]=buf[i];

      i++;

      }

    lno[i]='\0';

//puts(lno);///

    }



i++;

oldi=i;

ch=buf[i];

switch(ch)

{

case 'x': xyzstart(); break;

default: printf("Nothing found");
}

}



fclose(fpo);

fclose(fp);

return 0;

}

I really don't know what to do!! Can anyone please help me out?? Thanks in advance!

Here's the code for both clear and xyzstart() void clear()

{

memset(buf,'\0',sizeof(buf));

memset(lno,'\0',sizeof(lno));

memset(def,'\0',sizeof(def));

memset(ref,'\0',sizeof(ref));

i=oldi=0;

memset(str,'\0',sizeof(str));

}

void xyzstart()

{

r=d=c=0;

for(;;c++,i++)

       {

           if(buf[i]==' ')

           break;

           if(buf[i]=='(') break;

           if(buf[i]==';')break;

           if(buf[i]=='\n') break;

           if(buf[i]=='=') break;

           if(buf[i]=='+' || buf[i]=='-') break;

           str[c]=buf[i];

        }

        str[c]='\0';

if(buf[i]=='=')

           assignment();

else if(buf[i]=='+' || buf[i]=='-') //Increments or decrements

            incdec();

          else if(buf[i]=='(')

                udefined();

}

解决方案

Some notes:

  1. Do not use feof(fp) as your while loop condition; the function will only return true after you try to read past the end of the file, so you'll wind up looping once too often. Check the result of your input operation (fgets will return NULL on failure) and then test for EOF, like so:

    
    while(fgets(buf, sizeof buf, fp) != NULL)
      count++;
    if (feof(fp))
      printf("At end of file\n");
    

  2. NEVER NEVER NEVER NEVER NEVER use gets: it will introduce a point of failure in your program (and probably has in this case). It has been deprecated as of C99 and is expected to be gone from the next version of the language (yes, the mayhem caused by this one library call is scarier than the prospect of breaking over 30 years of legacy code). Use fgets or some other alternative instead.

From the code you've posted, I see no obvious issues; I don't know why feof would core dump if the previous statement was a successful rewind. The only thing I can figure is that the file pointer is being overwritten somewhere (possibly by a buffer overrun in a gets call).

这篇关于帮助需要关于在文件指针分割故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆