Help on file reading with nested array structres

This is a discussion on Help on file reading with nested array structres within the C Programming forums, part of the General Programming Boards category; Hello. I am trying to print out the entire contents of a file. I have to read the contents and ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    Help on file reading with nested array structres

    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:

    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);
    }
    Here are the contents of the file:
    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

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,291
    Several problems here:
    1. It's int main(void) and return an integer at the end, usually 0.
    2. Don't control a loop with feof.
    3. scanf and printf are similar. You only need one format string for all your specifiers in you fscanf call, like you did in printf. "%s %c %s..."
    4. The initial member of your student struct doesn't need to be an array since it's just one character. Drop the [MAX]
    5. You don't need the & for any strings/character arrays (where you store a %s conversion). The name of the array serves as the address to the first element.
    6. I don't know what fscanf_s returns, but if it's anything like fscanf, you should check the return value (read your documentation) to make sure it properly read all the fields.

    That's all I got for now. Some of those items should show up as errors/warnings when you compile. Crank up the settings on your compiler if you can, and make sure you always resolve all warnings since they usually signal problematic code.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Why are you nesting structs? It makes no sense... just put the data from the address into the main struct... Not only is it simpler programming it's going to be a lot easier to read and write from disk.

    Also why do yo uneed 50 characters for a zip code or a middle intial... you need to size things more appropriately. And I seriously doubt you need an 8 byte floating point number to represent a GPA that likely runs 0.0 to 5.0; use a char and multiply by 10... i.e. a 3.8 GPA is stored as 38.
    Last edited by CommonTater; 05-09-2011 at 12:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from file into Array
    By lvl99 in forum C Programming
    Replies: 8
    Last Post: 09-23-2009, 02:27 PM
  2. Do structres get zeroed at declaration?
    By Govalant in forum C Programming
    Replies: 7
    Last Post: 07-05-2007, 01:35 PM
  3. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  4. Reading file into a 2d array
    By beginner999 in forum C Programming
    Replies: 7
    Last Post: 01-08-2003, 07:19 AM
  5. heeeeeeeeelp with file structres
    By NANO in forum C++ Programming
    Replies: 4
    Last Post: 11-14-2002, 01:03 PM

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