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:
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 likeCode:#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; }before the loop andCode:int i = 0;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.Code:i++; if(i == 20) break;
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.