Segmentation fault on fclose

This is a discussion on Segmentation fault on fclose within the C Programming forums, part of the General Programming Boards category; Why am I getting a "Segmentation fault" error? I tried out gdb and determined that the fclose command is causing ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    19

    Segmentation fault on fclose

    Why am I getting a "Segmentation fault" error? I tried out gdb and determined that the fclose command is causing the error.

    Code:
    > cat fopen.c#include <stdio.h>
    int main()
    {
      FILE *fp;
      fp=fopen("bak/test.txt", "r");
      printf("Hello, World!\n");
      fclose(fp);
      return 0;
    }
    > gcc -g -o fopen fopen.c
    > ./fopen
    Hello, World!
    Segmentation fault
    Thanks for help.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You aren't testing that you actually opened the file. Perhaps you're fclose() is being down on a file that was never opened?

    Test the file pointer directly, instead of using redirection, to see if it's open.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    19
    Can I retract this (very embarrassing) question? Note that there is no file named "test.txt" in the directory named "bak" below the directory I'm executing from. You think that could cause it? Hmm? Maybe? (Hint: The answer is yes).

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    19
    Quote Originally Posted by Adak View Post
    You aren't testing that you actually opened the file. Perhaps you're fclose() is being down on a file that was never opened?

    Test the file pointer directly, instead of using redirection, to see if it's open.
    As long as you answered before I could retract this, how do you "test the file pointer directly"? I'm only up to the heading fclose C File I/O Tutorial - Cprogramming.com on this tutorial. That tutorial doesn't provide an example of testing the file pointer.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    fopen() returns NULL to the file pointer if the file can't be opened for any reason.

    So
    if(fp == NULL) or simply if(!fp) { //file didn't open, print an error message and direct the program accordingly. (program stops, program tries again, etc.).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 07-08-2012, 03:11 PM
  2. Segmentation Fault
    By SterlingM in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2010, 09:27 AM
  3. arg SEGMENTATION FAULT!
    By rocomotion in forum C Programming
    Replies: 2
    Last Post: 09-23-2009, 06:27 AM
  4. fclose operation giving seg fault
    By ritu85 in forum C Programming
    Replies: 14
    Last Post: 07-23-2009, 02:54 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21