Cant close file pointer

This is a discussion on Cant close file pointer within the C Programming forums, part of the General Programming Boards category; Hi i get a segmentation fault when i want to close a file i have opened here is the code ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    31

    Question Cant close file pointer

    Hi

    i get a segmentation fault when i want to close a file i have opened
    here is the code

    Code:
    char *temp;
    
    temp = (char *)malloc(1000);
    
    .......
    
            if ((fi = fopen(fiPath, "r")) == NULL)
            {
                fprintf(stderr, "Error reading file: %s\n", fiPath);
                fo = NULL;
                return;
            }
            /* copy the whole file into a temp memory location */
            while (fgets(line_buffer, 80, fi) != NULL)
                strcat(temp, line_buffer);
            fclose(fi);
            free(temp);
    it seg faults on fclose and i cant free temp

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Consider posting a working (or in this case, non working), compilable example, instead of some random crap. Also, instead of strcating everything over and over, instead, ignore allocation, and simply print the line. My guess is you're running off the end of temp, and it just appears to fail on fclose or free.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    31
    Seems like there is a problem with the strcat

    How would you dump the entire file into memory?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It depends what you want to do with it. There are many ways. You could...

    a - Use realloc, and reallocate your buffer bigger and bigger each time, appending what you've read onto it.
    b - Use a linked list, store a...
    b1 - character per node.
    b2 - line per node.
    b3 - page per node.
    b4 - etc.
    c - Recursively read a character at a time, doing nothing more than displaying it in said recursive call. (Small files only. )
    d - Other fun things, left up for other readers...

    Really though, it depends on what you want to do with it. Try them all, if you're bored.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    First problem is you cast the return result of malloc.

    The second problem is that you assume the allocated memory has the first byte as \0, for strcat to work effectively with. malloc returns memory in an undefined state.

    The third problem is are you assuming that the file is always <1000 bytes?

    The Fourth problem is how did you declare line_buffer?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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