I'm having a problem maintaining the data in the file. Everytime I enter a person's data the display code I have only displays the first one I entered. I'm not sure if there's somethin wrong with the creating or populating or just the display code I'm using

void addReserve()
{
   char uoption;
   int option=0;
   int i=0;
   int count = 1;
   int res_num;
   FILE *reserver;
   srand(time(NULL));

   if((reserver=fopen("reservations.txt", "rb+"))==NULL)
   {
   	printf("\nFile could not be opened, file is now be created");
   	if((reserver=fopen("reservations.txt", "wb+"))==NULL)
      {
   		printf("Error in creatiing/opening file");
      	_getch();
      }
      else
      {
      	for (; i < 100; i++)
         {
         	fwrite( &section, sizeof( ADMISSION_DATA ), 1, reserver );
            printf("\nRecord # %d", count);
            count++;
         }
         fclose( reserver );
      }
   }
   fclose ( reserver );

   if((reserver=fopen("reservations.txt", "rb+"))==NULL)
   {
    	printf("Error in creatiing/opening file");
   }
   else
   {
		printf("\nWelcome to the Reservation Section!");

		do{
            printf("\nEnter a reservation number: ");
            scanf("%d", &res_num);

            fseek( reserver, (res_num - 1) * sizeof ( ADMISSION_DATA ), SEEK_SET );

            fread( &section, sizeof( ADMISSION_DATA ), 1, reserver );

            if(section.reservation_num != 0)
            {
            	printf("\nA reservation already exits in this slot");
            }
            else
            {
            section.reservation_num = res_num;

   			section.guest_num = rand()% (30000 - 1 + 1) + 1;
  				
				printf("\nEnter First Name: ");
   			scanf("%s",&section.first_name);

         	printf("\nEnter Middle Initial: %c");
         	scanf("%s", &section.MI);

         	printf("\nEnter Last Name: ");
   			scanf("%s",&section.last_name);

            printf("\nEnter credit card number: ");
  				scanf("%d",&section.credit_card);

   	 		printf("\nContact's Cell/Home/Work #: ");
   			scanf("%d", &section.telephone);

            printf("\nEnter room number: ");
            scanf("%d", &section.room_connec.room_num);

            printf("\nEnter room type (1 for smoking and 0 non-smoking): ");
         	scanf("%d", &section.room_connec.room_type);   //1-smoking 0-non-smoking

            room.room_status=9;//9-reserve 10-vacant 12-occupied

            fseek( reserver, ( section.guest_num - 1 ) * sizeof( ADMISSION_DATA ), SEEK_SET );

         	fwrite( &section, sizeof( ADMISSION_DATA ), 1, reserver );

         	fclose(reserver);
            }
            reserver = fopen( "reservations.txt", "rb");
            while (!feof( reserver ) )
      		{
         		fread( &section, sizeof ( ADMISSION_DATA ), 1, reserver);

            	if( section.reservation_num != 0)
         		{
         			printf( "%d\t%d\t%s\t%s\t%s\t%d\t%d\t%.2f\t%d\t%d\t%d\t%d\n", section.reservation_num, section.guest_num, section.first_name, section.MI, section.last_name, section.credit_card, section.telephone, section.charge, section.cancel, section.length_stay, section.room_connec.room_num, section.room_connec.room_type);
         		}
         	}
            section.reservation_num = 0;
         	printf("\n\nDo you wish to continue (y / n): ");
         	fflush(stdin);
         	scanf("%c",&uoption);
      }while(uoption!='n');
	}
   fclose ( reserver );
}

Look at the file in Notepad or some other text editor to see if the contents are what you expect.

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.