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
ThanksCode:#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 */
DMKanz07