i get a stupid seg fault when i try to open/read a file. all works well if the file == NULL and an error has to be printed, but it either just continuly loops or seg faults if the file is a good one. it usually stops on or around the scanf if i get a seg fault. plz take a look.
Code:FILE *in_file = NULL; /* Declare global batch read in file handler */ char user_file[STR_MAX]; /* Declare temporary variable to hold user input */ int goods = 0, bads = 0, eof_test = FALSE; /* Declare temporary counting variables */ int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0; /* Declare temporary counting variables */ printf("\nAutomatic Mode Is Running...\n\n"); /* Display opening message */ good_file = fopen("address_book.txt", "w"); /* Open file for correct output using write parameter */ bad_file = fopen("errors.txt", "w"); /* Open file for incorrect output using write parameter */ printf("Please enter the filename of your address book: >> "); /* Ask user for input file name */ scanf("%s", &user_file); /* Take line of input from user for file name */ if((in_file = fopen("a.txt", "r")) == NULL) /* Check if read file is corrupted */ { /* Start if statement */ printf("\nCannot open input file, please check the filename try again.\n\n"); /* Print error message */ exit(1); /* Exit after input file error */ } /* End if statement */ z = fgetc(in_file); /* Gather character from file to temporary variable */ while(eof_test == FALSE) /* Continue read loop while end of file is not found */ { /* Start while loop */ for(a = 0; a < MAX; a++) /* Run loop until maximum address book size */ { /* Start for loop */ rester(); /* Call to reset word test variables */ for(b = 0; b < UNIT_MAX; b++) /* Run loop until maximum unit size */ if((z == ' ') && (start == FALSE)) /* Check if read in character is a space */ { /* Start if statement */ b--; /* Reset counter back one to allow space */ } /* End if statement */ else if((z >= '0') && (z <= '9')) /* Check if first character is a house number */ { /* Start if statement */ fptr->number[a][0] = z; /* Move read in character into struct */ house = TRUE; /* Set test for house or unit to true */ break; /* Break from unit loop if its a house */ } /* End if statement */ else /* Else read in character is not a space */ { /* Start else statement */ start = TRUE; /* Set started to true if word has begun */ if((z == ' ') && (start == TRUE)) /* Check if character is a space and word has started */ { /* Start if statement */ end = TRUE; /* Set end to true if at end of word */ } /* End if statement */ else /* Else character is letter in word */ { /* Start else statement */ fptr->flat_unit[a][b] = z; /* Move read in character into struct */ } /* End else statement */ } /* End else statement */ z = fgetc(in_file); /* Gather character from file to temporary variable */ } /* End for loop */ .... .... ....



LinkBack URL
About LinkBacks


