Thread: Array of Structures

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    42

    Array of Structures

    Hello everyone,

    I am writing a program that use structures and I have a couple questions. First if I am reading the data from a text file in to an array of structure how is the data laid out in the text file (i.e. name, dob, height, etc.) And then secondly how do I read in the data. In function2 and 3 is where I am trying to read the data into the array from the text files.

    Here is my code so far

    Code:
    #include <stdio.h>
    #include <string.h>
    #define intiSize 0
    
    typedef struct { 
        char ( name [] );
        int dob;
        int height;
        char ( father [] );
        char ( mother [] );
    } horse;
    
    typedef struct { 
        char ( raceDay [] );
        int trackNum;
        char ( raceTime [] );
        char ( horses [] );
    } race;
    
    
    /* Function Prototype */
    int menu();
    void loadHorse( horse );
    void loadRace ( race );
    
    /* Function main begins program execution */
    int main()
    {
        horse horses [ intiSize ];
        horse *horsePtr = &horses [ 0 ];
        
        race races [ intiSize ];
        race *racePtr = &races [ 0 ];
        
        loadHorse( *horsePtr );
        loadRace ( *racePtr );
        menu();        
                  
        /* Indicates successful termination */
        return 0;
    
    } /* End Main */
    
    /* Function1 menu definition */
    int menu()
    {
        int option;
        
        while( option != 7 ){
            
            printf( "\n- Horse Farm Management System Main Menu -\n\n" );
            printf( "  1.)  Add a Horse\n" );
            printf( "  2.)  Add a Race\n" );
            printf( "  3.)  View Horse Information\n" );
            printf( "  4.)  Sort Horse Information\n" );
            printf( "  5.)  View Race Information by Track\n" );
            printf( "  6.)  View Race Information by Horse\n" );
            printf( "  7.)  Exit\n\n" );
            printf( "       Choose an option:  " );
            scanf( "%d", &option );
            
            if ( option == 1 )
                    printf( "\nCall Function Add_New_Horse\n" );
            else if ( option == 2 )
                    printf( "\nCall Function Add_New_Race\n" );
            else if ( option == 3 )
                    printf( "\nCall Function View_Horses\n" );
            else if ( option == 4 )
                    printf( "\nCall Function Sort_Horses\n" );
            else if ( option == 5 )
                    printf( "\nCall Function View_Race_Information_By_Track\n" );
            else if ( option == 6 )
                    printf( "\nCall Function View_Race_Information_By_Horse\n" );
        }
            
        return 0;
                    
    } /*End function menu */
    
    /* Function2 loadHorse definition */
    void loadHorse( horse horses )
    {
        
        char horses []; /* Data records */
        
        FILE *cfPtr; /* cfPtr = horses.txt pointer */
        
        /* Fopen opens file; creates file if cannot be opened */
        if ( ( cfPtr = fopen( "horses.txt", "r" ) ) == NULL ) {
            printf( "\nFile does not exist - Creating 'horses.txt'\n" );
                if ( ( cfPtr = fopen( "horses.txt", "w" ) ) == NULL ) {
                } /* End if */
        } /* End if */
        
        /* Read records from file */
        else {
    
            /* While not end of file */
            while( !feof( cfPtr ) ) {
                    fscanf( cfPtr, "%s", horses );
            } /* End while */
            
            fclose( cfPtr ); /* fclose closes the file */
            
        } /* End else */
        
    } /*End function menu */
    
    /* Function3 loadRace definition */
    void loadRace( race races )
    {
        
        char races []; /* Data records */
        
        FILE *cfPtr; /* cfPtr = races.txt pointer */
        
        /* Fopen opens file; creates file if cannot be opened */
        if ( ( cfPtr = fopen( "races.txt", "r" ) ) == NULL ) {
            printf( "\nFile does not exist - Creating 'races.txt'\n\n" );
                if ( ( cfPtr = fopen( "races.txt", "w" ) ) == NULL ) {
                } /* End if */
        } /* End if */
        
        /* Read records from file */
        else {
    
            /* While not end of file */
            while( !feof( cfPtr ) ) {
                    fscanf( cfPtr, "%s", races );
            } /* End while */
            
            fclose( cfPtr ); /* fclose closes the file */
            
        } /* End else */
        
    } /*End function menu */
    Thanks
    DMKanz07

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char ( name [] );
    What exactly are you hoping to achieve with this declaration?

    Perhaps begin with
    char name[50];
    and see how you get on with small data sets.
    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.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    First if I am reading the data from a text file in to an array of structure how is the data laid out in the text file
    If you're planning on reading and writing an entire structure at a time, you'll need to use a binary file. Use fread() and fwrite() to read and write the structures. Make sure you open the file in binary mode as well.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Quote Originally Posted by dmkanz07 View Post
    Hello everyone,

    First if I am reading the data from a text file in to an array of structure how is the data laid out in the text file (i.e. name, dob, height, etc.)
    DMKanz07
    If you mean, how the data which you are handling, is contained in the txt...
    As I know If your read-order is like so;
    name
    dob
    height
    father
    mother

    The pattern of txt should be accorded with your program, like;
    Horse 23 193 horsefather horsemother
    or vertical anyway,
    horse
    23
    193
    horsefather
    horsemother

    unless you are not using a binary fil *.dat...

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by itsme86 View Post
    If you're planning on reading and writing an entire structure at a time, you'll need to use a binary file. Use fread() and fwrite() to read and write the structures. Make sure you open the file in binary mode as well.
    He's not - as he stated, input will be a TEXT file.

    The input loops for your program need to match the input files data. No good asking us "how it's laid out in the text file", because we have no clue. Generally, input files in text mode use a space or newline between fields for the structure.

    You should have or make up a small input sample file for testing your program.

    Adak

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    I hope I am explaining this right (noob)

    What I am trying to do is read the data from a file into the structure defined as horse.

    So what I did is defined the structure
    Code:
    typedef struct { 
        char ( name [] );
        int dob;
        int height;
        char ( father [] );
        char ( mother [] );
    } horse;
    Then called the function loadHorse
    Code:
    /* Function2 loadHorse definition */
    void loadHorse( horse horses )
    {
        
        char horse [ 100 ]; /* Data records  - NOT SURE IF THIS IS RIGHT */
        
        FILE *cfPtr; /* cfPtr = horses.txt pointer */
        
        /* Fopen opens file; creates file if cannot be opened */
        if ( ( cfPtr = fopen( "horses.txt", "r" ) ) == NULL ) {
            printf( "\nFile does not exist - Creating 'horses.txt'\n" );
                if ( ( cfPtr = fopen( "horses.txt", "w" ) ) == NULL ) {
                } /* End if */
        } /* End if */
        
        /* Read records from file */
        else {
    
            /* While not end of file */
            while( !feof( cfPtr ) ) {
                    fscanf( cfPtr, "%s", horse );
            } /* End while */
            
            fclose( cfPtr ); /* fclose closes the file */
            
        } /* End else */
        
    } /*End function menu */
    But what I am unsure of is does this populate the elements and values of the arrary structure defined as horse? I hope this make sense.

    Here is my whole code so far. I did test it using a simple array and added a printf statement before the fscanf and it did print out the text file content.

    Code:
    #include <stdio.h>
    #include <string.h>
    #define intiSize 0
    
    typedef struct { 
        char ( name [] );
        int dob;
        int height;
        char ( father [] );
        char ( mother [] );
    } horse;
    
    typedef struct { 
        char ( raceDay [] );
        int trackNum;
        char ( raceTime [] );
        char ( horses [] );
    } race;
    
    
    /* Function Prototype */
    int menu();
    void loadHorse( horse );
    void loadRace ( race );
    
    /* Function main begins program execution */
    int main()
    {
        horse horses [ intiSize ];
        horse *horsePtr = &horses [ 0 ];
        
        race races [ intiSize ];
        race *racePtr = &races [ 0 ];
        
        loadHorse( *horsePtr );
        loadRace ( *racePtr );
        menu();        
                  
        /* Indicates successful termination */
        return 0;
    
    } /* End Main */
    
    /* Function1 menu definition */
    int menu()
    {
        int option;
        
        while( option != 7 ){
            
            printf( "\n- Horse Farm Management System Main Menu -\n\n" );
            printf( "  1.)  Add a Horse\n" );
            printf( "  2.)  Add a Race\n" );
            printf( "  3.)  View Horse Information\n" );
            printf( "  4.)  Sort Horse Information\n" );
            printf( "  5.)  View Race Information by Track\n" );
            printf( "  6.)  View Race Information by Horse\n" );
            printf( "  7.)  Exit\n\n" );
            printf( "       Choose an option:  " );
            scanf( "%d", &option );
            
            if ( option == 1 )
                    printf( "\nCall Function Add_New_Horse\n" );
            else if ( option == 2 )
                    printf( "\nCall Function Add_New_Race\n" );
            else if ( option == 3 )
                    printf( "\nCall Function View_Horses\n" );
            else if ( option == 4 )
                    printf( "\nCall Function Sort_Horses\n" );
            else if ( option == 5 )
                    printf( "\nCall Function View_Race_Information_By_Track\n" );
            else if ( option == 6 )
                    printf( "\nCall Function View_Race_Information_By_Horse\n" );
        }
            
        return 0;
                    
    } /*End function menu */
    
    /* Function2 loadHorse definition */
    void loadHorse( horse horses )
    {
        
        char horse [ 100 ]; /* Data records */
        
        FILE *cfPtr; /* cfPtr = horses.txt pointer */
        
        /* Fopen opens file; creates file if cannot be opened */
        if ( ( cfPtr = fopen( "horses.txt", "r" ) ) == NULL ) {
            printf( "\nFile does not exist - Creating 'horses.txt'\n" );
                if ( ( cfPtr = fopen( "horses.txt", "w" ) ) == NULL ) {
                } /* End if */
        } /* End if */
        
        /* Read records from file */
        else {
    
            /* While not end of file */
            while( !feof( cfPtr ) ) {
                    fscanf( cfPtr, "%s", horse );
            } /* End while */
            
            fclose( cfPtr ); /* fclose closes the file */
            
        } /* End else */
        
    } /*End function menu */
    
    /* Function2a loadRace definition */
    void loadRace( race races )
    {
        
        char race [ 100 ]; /* Data records */
        
        FILE *cfPtr; /* cfPtr = races.txt pointer */
        
        /* Fopen opens file; creates file if cannot be opened */
        if ( ( cfPtr = fopen( "races.txt", "r" ) ) == NULL ) {
            printf( "\nFile does not exist - Creating 'races.txt'\n\n" );
                if ( ( cfPtr = fopen( "races.txt", "w" ) ) == NULL ) {
                } /* End if */
        } /* End if */
        
        /* Read records from file */
        else {
    
            /* While not end of file */
            while( !feof( cfPtr ) ) {
                    fscanf( cfPtr, "%s", race );
            } /* End while */
            
            fclose( cfPtr ); /* fclose closes the file */
            
        } /* End else */
        
    } /*End function menu */
    Thank for the patience

    DMKanz07

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    Okay I tried this but when I compile I get a parse error. I am trying to take the records in horses.txt and pass them to the structure horse and then print the structure to make sure it is passing the right information (print statement not shown not sure where to place).
    Code:
    #include <stdio.h>
    
    /* Horse structure definition */
    struct horse {
        char ( name [] );
        int dob;
        int height;
        char ( father [] );
        char ( mother [] );
    
    }; /* End structure horse */
    
    /* New type name for struct horse */
    typedef struct horse Horse;
    
    /* Function prototype */
    void loadData();
    
    /* Function main begins program execution */
    int main()
    {
    
        loadData();
    
        /* Indicates successful termination */
        return 0;
    
    } /* End Main */
    
    /* Function2 loadData definition */
    void loadData()
    {
    
        int i;
        FILE *cfPtr; /* cfPtr = horses.txt pointer */
        
        /* Fopen opens file; creates file if cannot be opened */
        if ( ( cfPtr = fopen( "horses.txt", "r" ) ) == NULL ) {
            printf( "\nFile does not exist - Creating 'horses.txt'\n" );
                if ( ( cfPtr = fopen( "horses.txt", "w" ) ) == NULL ) {
                } /* End if */
        } /* End if */
        
        /* Read records from file */
        else {
        
        i = 0;
        
            /* While not end of file */
            while( !feof( cfPtr ) ) {
                    fscanf( cfPtr, "%s", Horse [ i ].name );
                    fscanf( cfPtr, "%s", Horse [ i ].dob );
                    fscanf( cfPtr, "%s", Horse [ i ].height );
                    fscanf( cfPtr, "%s", Horse [ i ].father );
                    fscanf( cfPtr, "%s", Horse [ i ].mother );
                    i++;
            } /* End while */
    
        fclose( cfPtr ); /* fclose closes the file */
        
    } /*End function loadData */
    Thanks

    DMKanz07

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct horse {
        char ( name [] );
        int dob;
        int height;
        char ( father [] );
        char ( mother [] );
    
    }; /* End structure horse
    What are those parenthesis supposed to be doing? Also, why don't you have sizes for any of your arrays?

    In the future, try actually paying attention to the error. They will usually tell you what line, or close to it, the error is on.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    Sorry about that I was focusing so much on the while loop I didnt check my syntax.

    Here is my code after checking it. It compiles but I get a windows error when I try to run it. Exer1_w8b.exe has encountered a problem and needs to close.

    Code:
    #include <stdio.h>
    
    /* Horse structure definition */
    struct horse {
        char name [ 50 ];
        int dob;
        int height;
        char father [ 50 ];
        char mother [ 50 ];
    
    }; /* End structure horse */
    
    /* New type name for struct horse */
    typedef struct horse Horse;
    
    /* Function prototype */
    void loadData();
    
    /* Function main begins program execution */
    int main()
    {
        
        loadData();
    
        /* Indicates successful termination */
        return 0;
    
    } /* End Main */
    
    /* Function2 loadData definition */
    void loadData()
    {
        
        /* Array that holds fifty horses */
        Horse myHorse [ 50 ];
        
        int i;
        FILE *cfPtr; /* cfPtr = horses.txt pointer */
        
        /* Fopen opens file; exits program if file if cannot be opened */
        if ( ( cfPtr = fopen( "horses.txt", "r" ) ) == NULL ) {
            printf( "\nFile does not exist\n" );
        } /* End if */
        
        /* Read records from file */
        else {
        
        i = 0;
        
            /* While not end of file */
            while( !feof( cfPtr ) ) {
                    fscanf( cfPtr, "%s", myHorse [ i ].name );
                    fscanf( cfPtr, "%s", myHorse [ i ].dob );
                    fscanf( cfPtr, "%s", myHorse [ i ].height );
                    fscanf( cfPtr, "%s", myHorse [ i ].father );
                    fscanf( cfPtr, "%s", myHorse [ i ].mother );
                    i++;
            } /* End while */
    
        fclose( cfPtr ); /* fclose closes the file */
        
        } /*End else */
        
    } /*End function loadData */
    Thanks

    DMKanz07

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by dmkanz07 View Post
    Okay I tried this but when I compile I get a parse error. I am trying to take the records in horses.txt and pass them to the structure horse and then print the structure to make sure it is passing the right information (print statement not shown not sure where to place).
    Code:
    #include <stdio.h>
    
    /* Horse structure definition */
    struct horse {
        char ( name [] );
        int dob;
        int height;
        char ( father [] );
        char ( mother [] );
    
    }; /* End structure horse */
    
    /* New type name for struct horse */
    typedef struct horse Horse;
    
    /* Function prototype */
    void loadData();
    
    /* Function main begins program execution */
    int main()
    {
    
        loadData();
    
        /* Indicates successful termination */
        return 0;
    
    } /* End Main */
    
    /* Function2 loadData definition */
    void loadData()
    {
    
        int i;
        FILE *cfPtr; /* cfPtr = horses.txt pointer */
        
        /* Fopen opens file; creates file if cannot be opened */
        if ( ( cfPtr = fopen( "horses.txt", "r" ) ) == NULL ) {
            printf( "\nFile does not exist - Creating 'horses.txt'\n" );
                if ( ( cfPtr = fopen( "horses.txt", "w" ) ) == NULL ) {
                } /* End if */
        } /* End if */
        
        /* Read records from file */
        else {
        
        i = 0;
        
            /* While not end of file */
            while( !feof( cfPtr ) ) {
                    fscanf( cfPtr, "%s", Horse [ i ].name );
                    printf("\n %s", Horse[i].name);
                    /* etc. for all members */
                    fscanf( cfPtr, "%s", Horse [ i ].dob );
                    fscanf( cfPtr, "%s", Horse [ i ].height );
                    fscanf( cfPtr, "%s", Horse [ i ].father );
                    fscanf( cfPtr, "%s", Horse [ i ].mother );
                    i++;
            } /* End while */
    
        fclose( cfPtr ); /* fclose closes the file */
        
    } /*End function loadData */
    Thanks

    DMKanz07
    Two questions:

    1) I see the definition for Horses, but I don't see the array of Horses structs, that your program will need. Just treat the array, like any other array, basically.

    2) This code does nothing:
    Code:
    if ( ( cfPtr = fopen( "horses.txt", "w" ) ) == NULL ) {
                } /* End if */
    Do you want to add a "return 1; onto it, perhaps? Show that the horses.txt file could not be opened?

    What about placing temporary print statements right after the data is read from the file, for each structure member? Just make sure you're getting the data you expect to get from the file. Fscanf() can be quite unforgiving if the data is anything you don't expect.

    Adak

  11. #11
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    This looks bad to me:
    Code:
    while( !feof( cfPtr ) )
    FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  12. #12
    Registered User
    Join Date
    Mar 2007
    Posts
    42

    Different Route

    Okay guys I went a different direction. But I have a question about using a binary file. If I want to create horses.dat what format do I need to delimit the individial strings.

    Here is my code
    Code:
    #include <stdio.h>
    
    /* Horse structure definition */
    struct horse {
        char name [ 50 ];
        int dob;
        int height;
        char father [ 50 ];
        char mother [ 50 ];
    
    }; /* End structure horse */
    
    /* Function main begins program execution */
    int main()
    {
        
        /* Horses.dat file pointer */
        FILE *cfPtr;
        
        /* Create horse with default information */
        struct horse Horse = { "", 0, 0, "", "" };
        
        /* FOpen opens the file; exits if file cannot be opened */
        if ( ( cfPtr = fopen( "horses.dat", "rb" ) ) == NULL ) {
            printf( "File could not be opened.\n" );
        } /* End if */
        else {
            printf( "%-8s%-17s%-10s%-10s%-10s\n", "Name", "Date of Birth",
                    "Height", "Father", "Mother" );
            
            /* Read all records from file ( until end of file ) */
            while ( !feof( cfPtr ) ) {
                    fread( &Horse, sizeof( struct horse ), 1, cfPtr );
                    
                    /* Display Record */
                    if ( Horse.name != "" ) {
                         printf( "%-8s%-17s%-10s%-10s%-10s\n", Horse.name, 
                             Horse.dob, Horse.height, Horse.father,
                             Horse.mother );
                    } /* End if */
                    
            } /* End while */
            
            /* Fclose close the file */
            fclose( cfPtr );
        
        } /* End else */
    
        /* Indicates successful termination */
        return 0;
    
    } /* End Main */
    Thanks

    DMKanz07

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you're using fixed size records, which is the easiest thing to do, just fread and fwrite the whole structure.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  2. filling an array of structures?
    By voodoo3182 in forum C Programming
    Replies: 9
    Last Post: 08-06-2005, 05:29 PM
  3. Array of Structures: Sorting
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-13-2005, 09:55 AM
  4. Filling an Array of Structures
    By Zildjian in forum C Programming
    Replies: 5
    Last Post: 11-12-2003, 05:54 PM
  5. Need serious help on array of structures
    By cwd in forum C Programming
    Replies: 2
    Last Post: 11-11-2001, 03:39 PM