/* Author : Michael Robinson Program : structs.c Purpose : To present strucs in C Updated : April 5, 2011 */ #include #include #include #define STRSIZE 10 FILE *inp; // pointer to the file inp FILE *outp; // pointer to the file outp typedef struct //this is a template to create multiple structures with diff names { //it does NOT allocate memory char name[STRSIZE]; double diameter; //equatorial diameter in km int moons; //number of moons double orbit_time; //years to orbit sun once double rotation_time; //hours to complete one revolution on axis }planet_t; typedef struct //this is a hierarchical struct b/c it contains another struct inside { double diameter; planet_t planets[9]; //note this is another structure, an array of planet_t structures char galaxy[STRSIZE]; }solar_sys_t; void pause() { printf( "Press any key to continue" ); getchar(); } void printPlanet( planet_t pl ) { printf("\n Planet's name: %s\n", pl.name); printf(" Equatorial diameter is %.1f km.\n", pl.diameter); printf(" Number of moons %d\n", pl.moons); printf(" Time to complete one orbit of the sun %.2f\n", pl.orbit_time); printf(" Time to complete one rotation on axis %.4f\n", pl.rotation_time); } int compareStructures( planet_t one, planet_t two ) { //page 414 return( strcmp(one.name, two.name) == 0 && one.diameter == two.diameter && one.moons == two.moons && one.orbit_time == two.orbit_time && one.rotation_time == two.rotation_time ); } void writeStructDataToDisk( planet_t one ) { outp = fopen("struct.txt", "w"); printf("\nWritting struct to disk file struct.txt\n"); fprintf(outp, "name : %s\n", one.name); fprintf(outp, "diameter : %f\n", one.diameter); fprintf(outp, "moons : %d\n", one.moons); fprintf(outp, "orbit_time : %f\n", one.orbit_time); fprintf(outp, "rotation_time : %f\n", one.rotation_time); printf( "\nname : %s\n", one.name); printf( "diameter : %f\n", one.diameter); printf( "moons : %d\n", one.moons); printf( "orbit_time : %f\n", one.orbit_time); printf( "rotation_time : %f\n", one.rotation_time); fclose(outp); } void readStructDataFromDisk() { char record[100]; int totalRecords = 0; int x = 0; int y = 0; inp = fopen("struct.txt", "r"); printf("\nGetting struct data from disk file struct.txt\n"); for(x=0; x < 5; x++) { printf("Record %d = ", totalRecords++ ); fgets (record , 100 , inp); printf("%s", record); } printf("\n"); fclose(inp); } int main() { //create structs from struct prototypes planet_t currentPlanet; planet_t previousPlanet; planet_t blankPlanet = {"blank", 0, 0, 0, 0}; //has been initialized with this data //load data into currentPlanet structure strcpy(currentPlanet.name, "Jupiter"); currentPlanet.diameter = 142980; currentPlanet.moons = 16; currentPlanet.orbit_time = 11.9; currentPlanet.rotation_time = 9.925; printf("\n currentPlanet: %s's equatorial diameter is %.1f km.\n", currentPlanet.name, currentPlanet.diameter); pause(); printf(" previousPlanet: %s's equatorial diameter is %.1f km.\n", previousPlanet.name, previousPlanet.diameter); pause(); //assign one struct to another printf("\n Assigning: previousPlanet = currentPlanet\n"); previousPlanet = currentPlanet; printf(" currentPlanet %s's equatorial diameter is %.1f km.\n", currentPlanet.name, currentPlanet.diameter); pause(); printf(" previousPlanet %s's equatorial diameter is %.1f km.\n", previousPlanet.name, previousPlanet.diameter); pause(); //print planets statistics printPlanet( currentPlanet ); printPlanet( previousPlanet ); printPlanet( blankPlanet ); int result = compareStructures( currentPlanet, previousPlanet ); if( result == 1 ) { printf("\n currentPlanet and previousPlanet Planets are equivalent\n"); } else { printf("\n currentPlanet and previousPlanet Planets are NOT equivalent\n"); } writeStructDataToDisk( currentPlanet ); readStructDataFromDisk(); exit(0); }//end int main()