Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6008439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:49:27+00:00 2026-05-23T01:49:27+00:00

I’ve a ‘C’ program which has encountered a strange problem.. I’m getting segmentation fault

  • 0

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();

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:49:28+00:00Added an answer on May 23, 2026 at 1:49 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.