Thread: Cant close file pointer

  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
    39,659
    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.

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, 10:39 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12: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, 10:51 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM