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 551227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:22:16+00:00 2026-05-13T11:22:16+00:00

Why is the following code giving segmentation fault? #include <stdio.h> #include <stdlib.h> int main()

  • 0

Why is the following code giving segmentation fault?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *file;
    file = fopen("text","r");
    if (file == NULL) printf("Error READING FILE");
    if (ferror(file)) printf("error reading file");    //line 9
    return 0;
}

Doing backtrace in gdb gives:-

> #0  0x00007ffff7ad9d30 in ferror () from /lib/libc.so.6
> #1  0x00000000004005fa in main () at test.c:9
  • 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-13T11:22:16+00:00Added an answer on May 13, 2026 at 11:22 am

    If fopen returns NULL, then the file isn’t open; you’re passing NULL in to ferror, which is invalid. You don’t have an open file to pass in; that’s what NULL means, that it couldn’t give you a file pointer. ferror is for getting errors related to reading and writing the file, once it has actually been opened and you have the file to work with.

    If fopen fails, and you want to get more information about why, you need to check the errno global variable, defined in errno.h.

    #include <errno.h>
    
    // ...snip...
    
    if (file == NULL) 
      printf("Error READING FILE: %s\n", strerror(errno));
    

    This example shows how to fetch a string describing the error; you could also compare the value in errno against one of the possible values it could have, and do something different depending on what the error is. See the fopen man page, or the POSIX spec, for a list of possible errors to compare against. Here’s how you could check against various possible errors:

    if (file == NULL) {
      int error = errno;  // copy it so other calls like printf don't modify it
      printf("Error READING FILE: %s\n", strerror(error));
      switch (error) {
      case EACCESS:
        // access was denied
        break;
      case ENOENT:
        // the file or one of its ancestors doesn't exist
        break;
        // etc...
      }
    }
    

    (this is an expansion of something I originally wrote in a comment on another answer)

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

Sidebar

Ask A Question

Stats

  • Questions 306k
  • Answers 306k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your Deferred class hasn't been defined - are there any… May 13, 2026 at 9:15 pm
  • Editorial Team
    Editorial Team added an answer Yes, but do you possibly also want to unset it… May 13, 2026 at 9:15 pm
  • Editorial Team
    Editorial Team added an answer Use custom inflections. See this related question: How do I… May 13, 2026 at 9:15 pm

Related Questions

Why is the following code giving an exception? function SetText(index, text) { try {
Ok so this is a newbie question on java, but i can't seem to
I am trying to read an image that resides somewhere on the web from
I have the following code: string acctStatus = account.AccountStatus.ToString(); if (!SettableStatuses().Any(status => status ==
Can you tell me why the following code is giving me the following error

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.