Google Answers Logo
View Question
 
Q: c programming code problem ( No Answer,   1 Comment )
Question  
Subject: c programming code problem
Category: Computers > Programming
Asked by: bcbak371-ga
List Price: $10.00
Posted: 01 Aug 2004 17:15 PDT
Expires: 31 Aug 2004 17:15 PDT
Question ID: 382161
I have written the following code. I am trying to write a statement to
delete or update an entry without changing the structure. Can anyone
show me how to locate a record for updating or deleting without
changing the structure. This is the code so far:
 #include <stdio.h>
#include <stdlib.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 */

   	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 );

          /* write user-specified information in file */
         fwrite( &person[i], sizeof( struct Person ), 1, fp );
         
         
   } /* end for */
	  
     fclose ( fp ); /* fclose closes the file */

   return 0; /* indicates successful termination */

} /* end main */
Answer  
There is no answer at this time.

Comments  
Subject: Re: c programming code problem
From: gposter-ga on 02 Aug 2004 07:27 PDT
 
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 */

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy