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