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

  • SEARCH
  • Home
  • 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 7894251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:20:50+00:00 2026-06-03T07:20:50+00:00

I have a function to read from a file: void input(char* buffer, FILE* fp)

  • 0

I have a function to read from a file:

void input(char* buffer, FILE* fp)
{
    char temp[100];
    fgets(temp,100,fp);
    strcpy(buffer,temp);
    buffer [strlen(buffer)-1]=0;
}

And I file:

1
2
3
4
5

The last line terminates with a newline.
I try to read the file this way:

FILE* fp=fopen("myfile.txt","r");
while(!feof(fp))
{
    char buffer[100];
    input(buffer,fp);
    puts(buffer);
}

It reads succesfully all the five strings: “1”,”2″,”3″,”4″,”5″ and prints them, but after the 5th string is printed, I get segmentation fault.

  • 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-06-03T07:20:52+00:00Added an answer on June 3, 2026 at 7:20 am

    This is a common anti-pattern in C (and other languages, really) to set up a loop like yours that checks !feof(fp). The problem is that this function returns true only after a read has failed due to EOF. The issue is that you use the results of your read before you check if the read even succeeded.

    I suggest you check the return value of fgets(), it will return NULL if there is an EOF. Propagate that value back and return it from your input() function. Then use that in your loop condition.

    Just to add, the seg fault itself is likely in your call to strcpy(). When fgets() fails, you attempt to copy a buffer of uninitialized data, which could contain literally anything and strcpy() may find a “string” much longer than your buffer. Even if it happens to find something shorter, your result will still be garbage.

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

Sidebar

Related Questions

I have function: char *zap(char *ar) { char pie[100] = INSERT INTO test (nazwa,
I'm trying to read from a text file using the read function and store
I'm trying to create a function to read Morse code from one file, convert
Hi there I have to read data from text file into the array in
I have an unmanaged dll that contains a function to read a data from
I'm trying to read from a file some data I already have and to
I have an array of strings that has been read from a file. At
Working on a problem where I have to read data from a file into
I have a function that calls out a read or write request on a
i have this function pymssql.connect(host=my host,user=my user,password=my pass,database=mydb) I want to read the user

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.