Hello, my problem deals with File Processing. The program I have creates a database and lets me input data for update. I have a problem creating the file correctly.
I need to keep a database in a binary file and update it everytime the user runs the program. My problem occurs when I run the program multiple times. When I run the program the first time, it runs through both if statements and the file is created; but ends without proceeding to the following else statement. When I run the program again from then on, it runs correctly as intended. My problem lies within the creation of the file. I need to create the file on the first run and have it advance to the else statement so I can proceed through the program; and make sure all of the data is saved into the database for the next time I run the program.
Example: I run the program and add an axe into the database.
The next time I run the program, I should have a axe in my database.
Code:
FILE *cfPtr;
int choice;
if ( ( cfPtr = fopen( "hardware.dat", "rb+" ) ) == NULL ) {
printf( "File could not be opened. \n" );
if ( ( cfPtr = fopen( "hardware.dat", "wb+" ) ) != NULL )
printf( "Created. \n" );
}
else {
while ( ( choice = enterChoice() ) != 5 ) {
switch ( choice ) {
case 1:
listInventory( cfPtr );
break;
case 2:
updateRecord( cfPtr );
break;
case 3:
newRecord( cfPtr );
break;
case 4:
deleteRecord( cfPtr );
break;
default:
printf( "Incorrect choice \n" );
break;
}
}
fclose( cfPtr );
}
system("pause");
return 0;
}