Below is an extension to your program that allows you to add, delete,
list and update records without changing the structure. You will need
to add error trapping and user input checking.
--
gp
-------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
/* person structure definition */
struct Person {
char lastName[ 15 ]; /* last name */
char firstName[ 15 ]; /* first name */
char age [ 4 ]; /* age */
}; /* end structure person */
int main()
{
int i, /* counter used to count records from 1-100 */
j; /* counter used to count 10 records */
/* create Person with default information */
struct Person person[100] = { "unassigned", "", "0" };
FILE *fp; /* nameage.dat file pointer */
/* fopen opens the file; exits if file cannot be opened */
if ( ( fp = fopen( "nameage.dat", "wb" ) ) == NULL ) {
printf( "File could not be opened.\n" );
} /* end if */
else {
/* output 100 blank records to file */
for ( i = 0; i <= 100; i++ ) {
fwrite( &person, sizeof( struct Person ), 1, fp );
} /* end for */
} /* end else */
// **********************************
// I think you wanted to complete the first ten records
// **********************************
fseek( fp , 0 , SEEK_SET );
printf("Input 10 records\n");
for( j= 0; j < 10; j++){
/* prompt user for last name, first name and age */
printf( "Enter lastname, firstname, age\n?");
/* set record lastName, firstName and age */
//fscanf( stdin, "%s%s%s", person[i].lastName,
//person[i].firstName, person[i].age, 100 + j );
fscanf( stdin, "%s%s%s",
person[i].lastName,
person[i].firstName,
person[i].age );
/* write user-specified information in file */
fwrite( &person[i], sizeof( struct Person ), 1, fp );
} /* end for */
fclose ( fp ); /* fclose closes the file */
// --------------------------------------------
// New Code
// --------------------------------------------
// We now have a 100 record file (nameage.dat)
// The first 10 records *may* have data, the other 90 do not
// Without changing the record structure or the file structure
// Allow updates or deletion to a record.
// POTENTIAL PROBLEM:
// Age is being stored as a 4 char array...consider storing as an int.
const int MAX_RECORDS = 100;
FILE* fpnew = fopen( "nameage.dat", "r+b" );
if( NULL == fpnew )
{
fprintf( stderr , "Failed to open file\n");
exit( -1 );
}
bool finished = 0;
while( !finished )
{
puts( "\nOptions\n=======\nl - list records\nd - delete record\nu -
update record\na - add record\nq - quit\n");
//int input = getchar();
int input = _getch();
switch(input)
{
case 'l':
case 'L':
{
fseek( fpnew , 0 , SEEK_SET );
struct Person p;
int idx;
for( idx = 0 ; idx < MAX_RECORDS ; idx++ )
{
// List only unassigned records
fread( &p , sizeof( Person) , 1 , fpnew );
if( strcmp( "unassigned" , p.lastName ) )
{
printf( "%d) %s %s %s\n" , idx , p.lastName , p.firstName , p.age );
}
}
break;
}
case 'd':
case 'D':
{
int recnum;
struct Person blank_person = { "unassigned" , "" , "0" };
printf( "Enter record number to delete: " );
fscanf( stdin, "%d" , &recnum );
if( recnum >= 0 && recnum < MAX_RECORDS )
{
fseek( fpnew , recnum * sizeof(Person) , SEEK_SET );
fwrite( &blank_person , sizeof( Person ) , 1 , fpnew );
}
break;
}
case 'u':
case 'U':
{
int recnum;
printf( "Enter record number to update: " );
fscanf( stdin, "%d" , &recnum );
struct Person update_person= { "" , "" , "0" };
if( recnum >= 0 && recnum < MAX_RECORDS )
{
printf( "Enter lastname, firstname, age\n?");
fscanf( stdin, "%s%s%s",
update_person.lastName,
update_person.firstName,
update_person.age );
fseek( fpnew , recnum * sizeof(Person) , SEEK_SET );
fwrite( &update_person, sizeof( Person ) , 1 , fpnew );
}
break;
}
case 'a':
case 'A':
{
fseek( fpnew , 0 , SEEK_SET );
struct Person p;
int idx;
for( idx = 0 ; idx < MAX_RECORDS ; idx++ )
{
// List only unassigned records
fread( &p , sizeof( Person) , 1 , fpnew );
if( 0 == strcmp( "unassigned" , p.lastName ) )
{
break;
}
}
struct Person add_person= { "" , "" , "0" };
printf( "Enter lastname, firstname, age\n?");
fscanf( stdin, "%s%s%s",
add_person.lastName,
add_person.firstName,
add_person.age );
fseek( fpnew , idx * sizeof(Person) , SEEK_SET );
fwrite( &add_person, sizeof( Person ) , 1 , fpnew );
break;
}
case 'q':
case 'Q':
{
finished = 1;
break;
}
}
}
// --------------------------------------------
return 0; /* indicates successful termination */
} /* end main */ |