Hello, the program I have creates a database and lets you update/add/delete any information you want about hardware items. My problem is.. after I exit the program, all the data is lost in the binary file. I need to keep a database in a binary file and update it everytime the user runs the program. I'm confused on how to go about that. example: I run the program and add a item called jigsaw, then exit.
The next time I run the program, I should have a jigsaw in my database.

My other problem is. I already designed the program to send the information to a text file, is there anyway to simply change that and just have the information stored in the file displayed in the program, not in a text file?

here is my program as of right now with information sending to text file and other problem

#include <stdlib.h>
#include <stdio.h>
#include<string.h>

struct hardwareData{
   int RecordNum;
   char ToolName[ 20 ];
   int quantity;
   double cost;
}; 

int enterChoice( void );
void textFile( FILE *readPtr );
void updateRecord( FILE *fPtr );
void newRecord( FILE *fPtr );
void deleteRecord( FILE *fPtr );
int main( void )
{
   FILE *cfPtr;
   int choice;  
   
   if ( ( cfPtr = fopen( "hardware.dat", "wb+" ) ) == NULL ) {
      printf( "File could not be opened. \n" );
   }
   else { 
        
      while ( ( choice = enterChoice() ) != 5 ) { 
        
         switch ( choice ) { 
            case 1:
               textFile( 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; 
} 

void textFile( FILE *readPtr )
{
   FILE *writePtr;
   struct hardwareData hardware = { 0, "", 0, 0.0 };
   
   if ( ( writePtr = fopen( "hardware.list.txt", "w" ) ) == NULL ) {
      printf( "File could not be opened. \n" );
   }
   else {
      rewind( readPtr );
      fprintf( writePtr, "%-12s%-20s%-11s%10s\n",
         "Record #", "Tool Name", "Quantity","Cost" );
         
      while ( fread( &hardware, sizeof( struct hardwareData ), 1, readPtr ) == 1 ){
        
        if (hardware.RecordNum != 0){
            fprintf( writePtr, "%-12d%-20s%-11d%10.2f\n", hardware.RecordNum, hardware.ToolName, hardware.quantity, hardware.cost );
               }
        }
   } 
       fclose( writePtr );
}
void updateRecord( FILE *fPtr )
{
   int record;
   int newquantity = 0;
   double newcost = 0;
   char NewToolName[20];
   struct hardwareData hardware = { 0, "", 0, 0.0 };
   
   printf( "Enter record to update ( 1 - 100 ): " );
   scanf( "%d", &record );
   
   fseek( fPtr, ( record - 1 ) * sizeof( struct hardwareData ),
      SEEK_SET );
   fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );
   
   if ( hardware.RecordNum == 0 ) {
      printf( "Record #%d has no information. \n", record );
   }
   else {
      printf( "%-12s%-20s%-11s%10s\n",
         "Record #", "Tool Name", "Quantity","Cost" );
      printf( "%-12d%-20s%-11d%10.2f\n\n", hardware.RecordNum, hardware.ToolName, hardware.quantity, hardware.cost );
               
      printf( "Enter new tool name (Without Spaces), Quantity, and Cost: " );
      scanf( "%s%d%lf", &NewToolName, &newquantity, &newcost ); 
      hardware.quantity = newquantity;
      hardware.cost = newcost;
      
      strcpy(hardware.ToolName, NewToolName);
          printf( "%-12s%-20s%-11s%10s\n", "Record #", "Tool Name", "Quantity", "Cost" );
      printf( "%-12d%-20s%-11d%10.2f\n", hardware.RecordNum, hardware.ToolName, hardware.quantity, hardware.cost );
      
      fseek( fPtr, ( record - 1 ) * sizeof( struct hardwareData ), SEEK_SET );     
      fwrite( &hardware, sizeof( struct hardwareData ), 1, fPtr );
   } 
}

/* delete an existing record */
void deleteRecord( FILE *fPtr )
{ 
   struct hardwareData hardware;
   struct hardwareData blankHardware = { 0, " ", 0, 0 };
   int RecordNum; 
   
   printf( "Enter record number to delete ( 1 - 100 ): " );
   scanf( "%d", &RecordNum );
   
   fseek( fPtr, ( RecordNum - 1 ) * sizeof( struct hardwareData ), SEEK_SET );
   fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );
   
   if ( hardware.RecordNum == 0 ) {
      printf( "Record %d does not exist.\n", RecordNum );
   }
   else { 
      fseek( fPtr, ( RecordNum - 1 ) * sizeof( struct hardwareData ), SEEK_SET );
      fwrite( &blankHardware, sizeof( struct hardwareData ), 1, fPtr );
   }
}
/* create new record */
void newRecord( FILE *fPtr )
{
   struct hardwareData hardware = { 0, "", 0, 0.0 };
   int RecordNum; 
   printf( "Enter new record number ( 1 - 100 ): " );
   scanf( "%d", &RecordNum );
   fseek( fPtr, ( RecordNum - 1 ) * sizeof( struct hardwareData ), SEEK_SET );
   fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );
  
   if ( hardware.RecordNum != 0 ) {
      printf( "Account #%d already contains information.\n",
      hardware.RecordNum );
   }
   else { 
      printf( "Enter tool name (Without Spaces), quantity, cost\n? " );
      scanf( "%s%d%lf", &hardware.ToolName, &hardware.quantity, &hardware.cost );
      hardware.RecordNum = RecordNum;
      
      fseek( fPtr, ( hardware.RecordNum - 1 ) * sizeof( struct hardwareData ), SEEK_SET );
      fwrite( &hardware, sizeof( struct hardwareData ), 1, fPtr );
   } 
}
int enterChoice( void )
{
   int menuChoice;
   printf( "\nEnter your choice\n"
      "1 - Store a formatted text file of acounts called\n"
      "    \"hardware.list.txt\" for printing\n"
      "2 - Update a record\n"
      "3 - Add a new record\n"
      "4 - Delete a record\n"
      "5 - End program\n? " );
   scanf( "%d", &menuChoice ); 
   return menuChoice;

}

The problem is the way the program opens the file on line 22 of the code you posted. "wb+" will delete the contents of the file if the file already exists. Read about the open flags here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.