Code:
#include <stdio.h>
/* structure defintions */
struct customerData {
int customerID;
char firstName [ 21 ];
char lastName [ 21 ];
int age;
char gender [2];
float weight;
float height;
int term;
};
/* prototypes */
void init (FILE *fPtr );
int enterChoice( void );
void createFile( FILE *readPtr );
void updateID( FILE *fPtr );
void newID( FILE *fPtr );
void deleteID( FILE *fPtr );
int main()
{
FILE *cfPtr; /* account.dat file pointer */
int choice; /* user choice */
/* fopens the file; exits if the file cannot be opened */
if ( ( cfPtr = fopen( "customers.dat", "rb+" ) ) == NULL ) {
printf( "File could not be opened.\n" );
} /* end if */
else {
/* enable user to specify action */
while ( ( choice = enterChoice() ) !=5 ) {
switch( choice ) {
/* create text file from record */
case 1:
createFile( cfPtr );
break;
/* update record */
case 2:
updateID( cfPtr );
break;
/* create record */
case 3:
newID( cfPtr );
break;
/* delete ID*/
case 4:
deleteID( cfPtr );
break;
/* display message if the user does not select a valid choice */
default:
printf( "Sorry wrong choice.\n" );
break;
} /* end switch */
} /* end while */
fclose( cfPtr ); /* fclose closes the file */
} /* end else */
return 0;
} /* end main */
void createFile( FILE *readPtr )
{
FILE *writePtr; /* customer.txt */
/* create customerData with default information */
struct customerData client = { 0, "", "", 0, "", 0.0, 0.0, 0};
/* fopen opens the file; exits if the file cannot be opened */
if ( ( writePtr = fopen( "customer.txt", "w" ) ) == NULL ) {
printf( "File could not be opened.\n" );
} /* end if */
else {
rewind( readPtr ); /* set the pointer to the beginning of the file */
fprintf( writePtr, "%s %s %d %s %f %f %d\n",
" Account #", "First name", "Last name", "Age", "Gender",
"Weight", "Height", "Membership Term" );
/* copy all records from random access file into text file */
while( !feof( readPtr) ){
fread( &client, sizeof( struct customerData ), 1, readPtr );
if( client.customerID !=0 ) {
fprintf( writePtr, "%s %s %d %s %f %f %d",
client.firstName, client.lastName, client.age,
client.gender, client.weight, client.height, client.term );
} /* end if */
} /* end while */
fclose( writePtr ); /* closes the file */
} /* end else */
} /* end function create file */
void updateID( FILE *fPtr )
{
int account; /* account number */
float ID; /* account transaction */
char firstName;
char lastName;
int age;
char gender;
float weight;
float height;
int term;
/* create customerData with no information */
struct customerData client = { 0, "", "", 0, "", 0.0, 0.0, 0};
/* obtain number of account to update */
printf( "Enter ID to update ( 101 - 200 ): ");
scanf( "%d", &account );
/* move file pointer to correct record in file */
fseek( fPtr, ( account - 101 ) * sizeof( struct customerData),
SEEK_SET );
/* read record from file */
fread( &client, sizeof( struct customerData ), 1, fPtr );
/* display error if ID does not exist */
if ( client.customerID == 0 ) {
printf( "Account #%d had no information.\n", & account );
} /* end if */
else { /* update ID */
printf( "%s %s %d %s %f %f %d\n\n",
client.customerID, client.firstName, client.lastName,
client.age, client.gender, client.weight, client.height,
client.term );
/* request transaction */
printf( "Please enter the account ID to update\n" );
scanf( "%d", &ID );
client.customerID += ID; /* account ID to update */
printf( "Please enter the account first name to update\n" );
scanf( "%s", &firstName );
client.firstName += firstName;
printf( "Please enter the account last name to update\n" );
scanf( "%s", &lastName );
client.lastName += lastName;
printf( "Please enter the client's age to update\n" );
scanf( "%d", &age );
client.age += age;
printf( "Please enter the client's gender to update\n" );
scanf( "%s", &gender );
client.gender += gender;
printf( "Please enter the client's weight to update\n" );
scanf( "%f", &weight );
client.weight += weight;
printf( "Please enter the client's height to update\n" );
scanf( "%f", &height );
client.height += height;
printf( "Please enter the clien't membership term to update\n" );
scanf( "%d", &term );
client.term += term;
printf( "%s %s %d %s %f %f %d\n\n",
client.customerID, client.firstName, client.lastName,
client.age, client.gender, client.weight, client.height,
client.term );
/* move pointer to correct record in the file */
fseek( fPtr, ( account - 101 ) * sizeof( struct customerData ),
SEEK_SET );
/* write updated record over the old record in file */
fwrite( &client, sizeof( struct customerData ), 1, fPtr );
} /* end else */
} /* end funtion updateID */
/* delete ID */
void deleteID( FILE *fPtr )
{
struct customerData client; /* stores record read from file */
struct customerData blankClient = { 0, "", "", 0, "", 0.0, 0.0, 0 };
int ID;
/* obtain number of the ID to delete */
printf( "Enter ID number to delete ( 101 - 200 ); " );
scanf( "%d", &ID );
/* move file pointer to the correct ID */
fseek( fPtr, ( ID - 101 ) * sizeof( struct customerData ),
SEEK_SET );
/* read record from file */
fread( &client, sizeof( struct customerData ), 1, fPtr );
/* display error if the record does not exist */
if ( client.customerID == 0 ) {
printf( "ID %d does not exist.\n", &ID );
} /* end if */
else { /* delete ID */
/* move file pointer to correct ID in the file */
fseek( fPtr, ( ID - 101 ) * sizeof( struct customerData ),
SEEK_SET );
/* replace existing record with blank record */
fwrite( &blankClient,
sizeof( struct customerData ), 1, fPtr );
} /* end else */
} /* end function deleteID */
void newID( FILE *fPtr )
{
/* create customerData with default information */
struct customerData client = { 0, "", "", 0, "", 0.0, 0.0, 0 };
int ID;
/* obtain ID from the account to create */
printf( "Enter the new ID number ( 101 - 200 ): " );
scanf( "%d", &ID );
/* move file pointer correct in record file */
fseek( fPtr, ( ID - 1) * sizeof( struct customerData ),
SEEK_SET );
/* read record from file */
fread( &ID, sizeof( struct customerData), 1, fPtr );
/* display error if the ID already exists */
if ( client.customerID !=0 ){
printf( "ID #%d already contains information.\n",
client.customerID );
} /* end if */
else { /* create ID */
/* user enters ID, first name, last name , age, gender, etc */
printf( "Enter the First name, last name\n?" );
scanf( "%s %s", &client.firstName, &client.lastName);
client.customerID = ID;
/* move file pointer to correct ID in the file */
fseek( fPtr, ( client.customerID -1 ) *
sizeof( struct customerData ), SEEK_SET );
/* insert ID in file */
fwrite( &client,
sizeof( struct customerData ), 1, fPtr );
} /* end else */
} /* end function newID */
/* enable user to choose the input menu option */
int enterChoice( void )
{
int menuOption ; /* variable to store the users choice */
/* display menu options */
printf( "\nEnter your choice:\n"
"1 - Calculate average.\n"
"2 - Add record.\n"
"3 - Change record.\n"
"4 - Delete record.\n"
"5 - Quit.\n" );
scanf( "%d", &menuOption );
return menuOption;
} /* end function enterChoice */