Hello. I am trying to print out the entire contents of a file. I have to read the contents and store them into an array of structures. when ever I run my program, It does not print out any thing and once it finishes the loop it crashes.
Please help.
Thank You.
Here is my code:
Here are the contents of the file:Code:#include <stdio.h> #include <string.h> #include <stdlib.h> //defines #define MAX 50 //structures typedef struct{ char street[MAX]; char city[MAX]; char state[MAX]; char zip[MAX]; }Address; typedef struct{ char firstName[MAX]; char initial[MAX]; char lastName[MAX]; Address sA; int age; double gpa; }Student; void main(void) { FILE *fp; Student students[MAX]; int count = 0; errno_t err; //error checking for the file. err = fopen_s(&fp, "Students.dat", "r"); // open the data file if (err != 0) { printf("File not found.\n"); exit(1); } while (!feof(fp)) { fscanf_s(fp, "%s", " %c", " %s", " %s", " %s", " %s", " %d", " %lf", &students[count].firstName, &students[count].initial, &students[count].lastName, &students[count].sA.street, &students[count].sA.city, &students[count].sA.state, &students[count].sA.zip, &students[count].age, &students[count].gpa); printf("%s, %3c, %6s, %9s, %8s, %2s, %2d, %2.2lf\n", students[count].firstName, students[count].initial, students[count].lastName, students[count].sA.street, students[count].sA.city, students[count].sA.state, students[count].sA.zip, students[count].age, students[count].gpa); ++count; } fclose(fp); }
ANNA A ADAMS 11 A STREET ALLENTOWN PA 11111 11 5.50
BOB B BRADBURY 22 B ROAD BOSTON MA 22222 22 2.22
CARLA C COTTRELL 33 C AVENUE CHICAGO IL 33333 33 3.33
DENNIS D DODD 44 D SQUARE DETROIT MI 44444 44 4.44
ERICA E EVANS 55 E STREET EUGENE OR 55555 55 9.99
FRANK F FIELDS 66 F ROAD FLAGSTAFF AZ 66666 66 6.66
GLENDA G GROGAN 77 G AVENUE GREAT FALLS MT 77777 77 7.77
HARRY H HALL 88 H WAY HONOLULU HI 88888 88 8.88
IDA I IFIELD 99 I STREET INDIANAPOLI IN 99999 99 1.11



LinkBack URL
About LinkBacks


