Segmentation fault (core dumped)

This is a discussion on Segmentation fault (core dumped) within the C Programming forums, part of the General Programming Boards category; I'm having a problem with a homework assignment, we have to write a program that opens a csv file with ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    28

    Segmentation fault (core dumped)

    I'm having a problem with a homework assignment, we have to write a program that opens a csv file with address information, and then it creates a new file for each zip code, and writes the addresses of into those files. First I'll post my code, then I'll explain what the issue is I'm having. Here's my code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
        FILE *listings, *file;
        listings = fopen("homelistings.csv", "r");
        while(!feof(listings)) {
            int zip, id, price, numofbeds, numofbaths, area;
            char address[50], filename[10];
            fscanf(listings, "%d, %d, %49[^,], %d, %d, %d, %d ", &zip, &id, address, &price, &numofbeds, &numofbaths, &area);
            printf("%d, %d, %s, %d, %d, %d, %d\n", zip, id, address, price, numofbeds, numofbaths, area); //This is just here so I could find where the issue is
            sprintf(filename, "%d.txt", zip);
            file = fopen(filename, "a");
            fprintf(file, "%s\n", address);
        }
        
        return 0;
    }
    OK, now my code is pretty simple, where it's failing is when it's looping through the lines of the csv file it crashes after line 1021, but what is really strange is if I have it loop up to that point or less (with something like
    Code:
    int i = 0;
    before the loop and
    Code:
    i++; if(i == 20) break;
    at the end of the loop) it successfully creates the zipcode files and writes the addresses to them, but if I have it try and run all the way through it creates all the files up to that point but doesn't write anything to any of them, which doesn't make any since to me. When it crashes it just says Segmentation fault (core dumped), and nothing more.

    I should also note, the csv file has almost 10,000 lines of data, so it's not making it all that far, and there are NO issues with the csv file, so it has nothing to do with that.

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    28
    Oh, geeze, I feel REALLY dumb now... I didn't close any of my files -_- I'm pretty new to C, but I know some Python, and I knew in Python you have to close the files you open, I should have realized that you'd have to close files in C too... All I had to do was add fclose(file); to the end of the loop and fclose(listings); before return 0;

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    28
    Here's my working code:

    Code:
    #include <stdio.h>
    
    int main() {
        FILE *listings, *file;
        listings = fopen("homelistings.csv", "r");
        while(!feof(listings)) {
            int zip, id, price, numofbeds, numofbaths, area;
            char address[50], filename[10];
            fscanf(listings, "%d, %d, %49[^,], %d, %d, %d, %d ", &zip, &id, address, &price, &numofbeds, &numofbaths, &area);
            sprintf(filename, "%d.txt", zip);
            file = fopen(filename, "a");
            fprintf(file, "%s\n", address);
            fclose(file);
        }
        
        fclose(listings);
        return 0;
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Do you get an extra copy of the last line?

    SourceForge.net: Feof - cpwiki
    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. Segmentation Fault (core dumped)
    By Kevin Jerome in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2012, 12:58 AM
  2. Segmentation Fault (core dumped)
    By sameer2904 in forum C Programming
    Replies: 3
    Last Post: 01-09-2012, 06:37 AM
  3. Segmentation fault (core dumped)
    By jonagondo in forum C Programming
    Replies: 6
    Last Post: 01-04-2012, 02:56 PM
  4. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 02:24 AM

Tags for this Thread


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