i'm writing a program that creates a file called 'hardware.dat' and i'm supposed to be able to insert a record, update, and delete a record. first i'm having problem with the insertrecord cause it keeps giving me an infinte loop. i'm creating file that would hold these data:
record # | tool name | quantity | cost
3 | electric sander | 7 | 57.98
17 | hammer | 76 | 11.99 etc
here is the code i have for the insert part
#include <stdio.h>
struct clientData
{
int recordNum;
char toolName[ 100 ];
int quantity;
double cost;
};//end struct
int enterChoice( void );
//void textFile( FILE *readPtr );
//void updateRecord( FILE *fPtr );
void insertRecord( FILE *fPtr );
//void deleteRecord( FILE *fPtr );
int main (void)
{
FILE *cfPtr;
int i;
int choice;
struct clientData blankClient = { 0, "", 0, 0.0 };
if( ( cfPtr = fopen( "hardware.dat", "rb+" ) ) == NULL )
{
printf("File could not be opened.\n" );
}//end if
else
{
for ( i = 1; i <= 100; i++ )
{
fwrite( &blankClient, sizeof(struct clientData), 1, cfPtr );
}//end for
while ( ( choice = enterChoice() ) != 1 )
{
switch ( choice )
{
case 1:
insertRecord( cfPtr );
break;
default:
printf( "Incorrect choice.\n" );
break;
}//end switch
}//end while
fclose (cfPtr);
}//end else
return 0;
}//end main
void insertRecord( FILE *fPtr )
{
struct clientData client = { 0, "", 0, 0.0 };
int recNum;
printf( "Enter new record # (1 - 100 ): " );
scanf( "%d", &recNum);
fseek( fPtr, ( recNum - 1 ) * sizeof( struct clientData ), SEEK_SET );
fread( &client, sizeof( struct clientData ), 1, fPtr );
if ( client.recordNum != 0 )
{
printf( "Account #%d already contains information.\n",
client.recordNum );
}//end if
else
{
printf( "Enter tool name, quantity and cost\n? " );
scanf( "%s%d%1f", &client.toolName, &client.quantity, &client.cost );
client.recordNum = recNum;
fseek( fPtr, ( client.recordNum - 1 ) *
sizeof( struct clientData ), SEEK_SET );
fwrite( &client, sizeof( struct clientData ), 1, fPtr );
}//end else
}//end function insert record
int enterChoice( void )
{
int menuChoice;
printf( "\nEnter your choice\n"
"1 - insert record\n"
"2 - end program\n? " );
scanf( "%d", &menuChoice );
return menuChoice;
}//end funtion enterCoice