Thread: closing a file! really odd problem!

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    closing a file! really odd problem!

    Code:
            FILE *g=fopen("out.txt","wt");
            fclose(g);
    i do this and it segmentation faults me! why?
    i can printf in the file just fine, but when i try to close it it gives me segmentation fault.. what could the problem be?

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Did you check to see if the fopen() call returned NULL or a valid pointer? fclose()ing NULL could be bad.
    It probably fails because of the "wt", I don't know what that "t" is doing there.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    t stands for text.
    and the pointer is not NULL

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1
    Hi All:
    Maybe you can alter you code int two line
    FILE *g;
    if ( ( g=fopen("out.txt","w")) != -1)
    fclose(g)

    I have not tried using declare a FILE pointer and point it out a file at the same time,
    at first. to know a FILE pointer is NULL or not, that is important.I feeling

    Thanks ALL

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > but when i try to close it it gives me segmentation fault.. what could the problem be?
    Maybe it calls malloc, and so do you, and unfortunately you've trashed the memory pool with some buffer overrun.

    Unless you can post a whole program which faults, then there isn't much we can do but make wild guesses.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    there was a free() problem. Any advice of links on this issue ? what did i do wrong when i used free and malloc ?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Any advice of links on this issue ?
    Only that you need to be more careful with the way you use memory.
    The fact that you used malloc doesn't make a difference, if you step off the end then bad things are going to happen. All that really changes when you do is how your program fails, not if/when your program fails.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  4. problem closing socket
    By Wisefool in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-29-2003, 12:19 PM
  5. problem closing a socket
    By Wisefool in forum C Programming
    Replies: 1
    Last Post: 10-28-2003, 01:38 PM