Thread: updating and deleting a record

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    1

    updating and deleting a record

    I need to be able to update a record and delete a record. My problem is that when I delete a record in only seems to delete the record in the 0th spot even if I put 1, 2, ect. Also, when I delete a record the output to the screen is not the same as it was before.

    In the delete function only deleted the item in the 0th spot and the remaining output changes.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX 100
    
    struct inventory {
      char sku[10];
      char description[30];
      double price;
      int quantity;
      
    } inv_list[MAX];
    
    void init_list(void),
         enter(void),
    	 create(void),     
    	 list(void);    
    	
    int menu_select(void),
    	update(void),
    	delete(void),
    	load(void),
        find_free(void),
        save(void);
    
    int main(void)
    {
      char choice;
    
      init_list(); /* initialize the structure array */
    
      for (;;)
      {
        choice = menu_select();
        switch(choice)
    	{
          case 1:
    		  enter();
              break;
          case 2: 
    		  delete();
              break;
          case 3:
    		  update();
    		  break;
    	  case 4:
    		  list();
              break;
          case 5:
    		  save();
              break;
          case 6:
    		  load();
              break;
          case 7:
    		  exit(0);
    		  break;
    	  default:
    		  printf("Invalid menu item selected.  Please try again\n\n");		  
    		  break;
        }
      }
    
      return 0;
    }
    
    /* Initialize the list. */
    void init_list(void)
    {
      register int t;
    
      for (t = 0; t < MAX; ++t)
    	inv_list[t].sku[0] = '\0';
    }
    
    /* Get a menu selection. */
    int menu_select(void)
    {
      char s[80];
      int c;
    
      printf("1. Enter a record \n");
      printf("2. Delete a record\n");
      printf("3. Update a record\n");
      printf("4. List the file\n");
      printf("5. Save the file\n");
      printf("6. Load the file\n");
      printf("7. Quit\n");
    
      do
      {
        printf("\nEnter your choice: ");
        gets(s);
        c = atoi(s);
      } while(c < 0 || c > 7);
    
      return c;
    }
    
    /* Input invesses into the list. */
    void enter(void)
    {
    // FILE *fPtr;
    	int slot;
      char s[80];
     // char answer;
    //  char output[80];
      slot = find_free();
    
      if (slot == -1)
      {
        printf("\nList Full");
        return;
      }
    
      printf("Enter sku: ");
      gets(inv_list[slot].sku);
    
      printf("Enter description: ");
      gets(inv_list[slot].description);
    
      printf("Enter price: ");
      gets(s);
      inv_list[slot].price = atof(s);
    
      printf("Enter quantity: ");
      gets(s);
      inv_list[slot].quantity = atoi(s);
    
      
    }
    
    /* Find an unused structure. */
    int find_free(void)
    {
      register int t;
    
      for (t = 0; inv_list[t].sku[0] && t < MAX; ++t) ;
    
      if (t == MAX)
    	return -1; /* no slots free */
    
      return t;
    }
    
    /* Delete a record. */
    int delete (void)
    {
     struct inventory delrec = {"","",0.00,0};
    
     FILE *fPtr;
      // register int inv;   
    //   char invty[10];  
       char output[80];
       char answer;
       int i = 0;
       int rec;
    
     
    	printf("Enter file name you want to delete a record from ");
    	gets(output);
    
    	  if ((fPtr = fopen(output, "r+")) == NULL)
    	  {
    		  printf("Cannot open file.\n\n");
    		  return 0;
    	  }
        
       printf( "Enter record # that you want to delete: " ); 
       scanf("%d", &rec);
       //gets(invty);   
       //inv = atoi(invty);
    
       rewind (fPtr);
       fseek( fPtr, ((rec-1)*62), SEEK_SET );
       fread(&inv_list[i], sizeof(struct inventory), 1, fPtr);
    
      	printf("Are you sure you want to delete information to record # %d", rec);
    	printf("Enter y for yes");
    	fflush(stdin);
    	scanf("%c", &answer);
    	
    	if (answer = 'Y')
    	{
    		rewind(fPtr);
    		fseek(fPtr, (rec-1)*sizeof(struct inventory), SEEK_SET);
    		fwrite(&delrec, sizeof(struct inventory), 1, fPtr);
    		printf("You record has been deleted");
    	}
    
       
       fclose(fPtr);
      // }
       return 0;
    }
    
    /* Display the list on the screen. */
    void list(void)
    {
      register int t;
    
      for (t = 0; t < MAX; ++t)
      {
        if(inv_list[t].sku[0])
    	{
          printf("%s\n", inv_list[t].sku);
          printf("%s\n", inv_list[t].description);
          printf("%f\n", inv_list[t].price);
          printf("%d\n\n", inv_list[t].quantity);
          
        }
      }
    
      printf("\n\n");
    }
    
    /* Save the list. */
    int save(void)
    {
      FILE  *fp;
      char output[80];  
      char reply[20];
    
    	register int i;
    
    	printf("Enter file you want to save to.  ");
    	gets(output);
    
      if ((fp = fopen(output, "w")) == NULL)
      {
        printf("Cannot open file.\n\n");
        exit(1);
      }
    
    if (fp != NULL)
    	{
    		printf("File aready exists.  Do you want to overwrite  Y or N: ");
    		gets(reply);
    
    		if (reply[0] == 'N' || reply[0] == 'n')
    		{
    			printf("Do you want to create a new file? ");
    			gets(reply);
    			
    			if (reply[0] = 'Y' || reply[0] == 'y')
    			{
    				create();
    			}
    		}
    		else
    		{
    
    }
    	
    
    for (i=0; i<MAX; i++)
    	{
    		if(inv_list[i].sku[0])
    		{
    			
    				if ((fprintf(fp, "%-09s", inv_list[i].sku))> 10)
    					
    			
    				{
    					fprintf(stderr, "     Unable to save to a file.  The sku is too long \n\n");
    				    fclose(fp);
    					return 0;
    				}
    
    				if ((fprintf(fp, "%-29s", inv_list[i].description)) > 30)
    				
    				{
    					fprintf(stderr, "     Unable to save to a file.  The description is too long\n\n");
    					
    				}
    				if ((fprintf(fp, "%.02f", inv_list[i].price)) > 10.2)
    				
    				{
    					fprintf(stderr, "     Unable to save to a file.  The price is too long\n\n");
    					fclose(fp);
    					return 0;
    				}
    				if ((fprintf(fp, "%08d\n",  inv_list[i].quantity)) > 9)
    				
    				{
    					fprintf(stderr, "     Unable to save to a file.  The quantity is too long\n\n");
    					fclose(fp);
    					return 0;
    			}			
    				
    		}
    }
    
    		 printf("File %s saved\n\n", output);
    		
    }
    
    
    		
    		fclose(fp);
    		return 0;
    		 
    }
    
    /* Load the file. */
    int load(void)
    {
      FILE  *fPtr;
    
      char output[80];
      char price[20];
      char quant[20];
      register int i;
     
      	printf("Enter the file name you want to open ");
    	gets(output);
    
    	  if ((fPtr = fopen(output, "r")) == NULL)
    	  {
    		  printf("Cannot open file.\n\n");
    		 exit(1);
    	  } 
    
    
      init_list();
    
    
    for(i=0;i<MAX; i++)
    	{	
    			if(! fgets(inv_list[i].sku, 10, fPtr))
    			{
    				if (feof(fPtr))
    				{
    					break;
    				}
    				fprintf(stderr, "     Unable to read File");
    				fclose(fPtr);
    				return 0;
    			
    			}
    
    			if (! fgets(inv_list[i].description, 30, fPtr))
    			{
    				if (feof(fPtr))
    				{
    					break;
    				}
    				fprintf(stderr, "     Unable to read File");
    				fclose(fPtr);
    				return 0;
    				
    			}
    
    			if (! fgets(price, 12, fPtr))
    			{
    				if (feof(fPtr))
    				{
    					break;
    				}
    				fprintf(stderr, "     Unable to read from the file");
    				fclose(fPtr);
    				return 0;				
    			}
    			inv_list[i].price = atof(price);
    
    			if (! fgets(quant, 8, fPtr))
    			{
    				if (feof(fPtr))
    				{
    					break;
    				}
    				fprintf(stderr, "     Unable to read from the file");
    				fclose(fPtr);
    				return 0;				
    			}
    			inv_list[i].quantity = atoi(quant);
    }
    		    
    		
    
      /*for (i = 0; i < MAX; i++)
        if (fread(&inv_list[i], sizeof(struct inventory), 1, fp) != 1)
    	{
          if (feof(fp))
    		break;
           
    	  printf("File read error.\n");
        }*/
    
      printf("File %s loaded and ready to be read from.\n\n", output);
      fclose(fPtr);
      return 0;
    }
    
    void create(void)
    {
    FILE *fPtr;
    	char output[80];
    	char reply [20];
    
    		printf("Enter the name of file you want to be created: ");
    		gets(output);
    		printf("\n");
    
    	if ((fPtr = fopen(output, "r+")) == NULL)
    	{
    		printf("File could not be created");
    		exit(1);
    	}
    		if ((fPtr = fopen(output, "r+"))!= NULL)
    		{
    			//fclose(fPtr);
    			printf("File Exist. Overwrite this file? (Y or N): ");
    			gets (reply);
    			if (reply[0] != 'Y' || 'y')
    			{
    				printf("%s file Overwritten\n\n", output);
    				
    			}	
    			else
    			{
    			printf("File '%s' Created\n\n", output);
    			}
    		}
    	    
    		if ((fPtr = fopen(output, "w")) == NULL)
    		{
    			printf("Unable to open file to write");
    		}
    }
    		
    
    int update(void)
    { 
       FILE *fPtr;
     register int inv;
    
       
       char invty[80];
       char output[80];
       int i = 0;
    
    	printf("Enter file name you want to update ");
    	gets(output);
    
    	  if ((fPtr = fopen(output, "r+")) == NULL)
    	  {
    		  printf("Cannot open file.\n\n");
    		  exit(1);
    	  }
      
      
       printf( "Enter record # that you want to update: " );   
       gets(invty);   
       inv = atoi(invty);
    
       rewind (fPtr);
       fseek( fPtr, ((inv-1)*62), SEEK_SET );
       fread(&inv_list[i], sizeof(struct inventory), 1, fPtr);
    
      	      printf("Enter new sku #: ");
    		  gets(inv_list[i].sku);
    		  		  
    		  printf("Enter new description: ");
    		  gets(inv_list[i].description);
    		  
    		  printf("Enter new price:  ");
    		  scanf("%10.2f\n", &inv_list[i].price);
    		  
    		  printf("Enter new quantity: ");
    		  scanf("%8\n", &inv_list[i].quantity);
    		   
    		  fseek(fPtr, ((inv-1)*62), SEEK_SET);
    		  fwrite( &inv_list[i], sizeof( struct inventory ), 1, fPtr );
     
       
       fclose(fPtr);
       return 0;
    }
    Thanks in advance
    Linda
    Last edited by Burritt; 04-20-2003 at 09:57 AM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You forgot the [/code] at the end. The lack of indentation hurts my eyes.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You may want to consider this and the links it references, but I didn't look into the code too closely.

    Avoid gets (it scares my linter). This is also discussed in the FAQ.

    This condition is always true in the function delete(). [I avoid delete as an identifier since it is a keyword in C++.]
    Code:
    if ( answer = 'Y' )
    You want == instead of =.

    Again, an always true condition in the function save().
    Code:
    if ( (fp = fopen(output, "w")) == NULL )
    {
       printf("Cannot open file.\n\n");
       exit(1);
    }
    
    if ( fp != NULL )
    If fp were NULL, you'd have exited.

    This format specifier doesn't make much sense in save().
    Code:
    if ( (fprintf(fp, "%-09s", inv_list[i].sku))> 10 )
    And again in save() you've got an always true assignment.
    Code:
    if ( reply[0] = 'Y' || reply[0] == 'y' )
    This is not right in create().
    Code:
    if ( reply[0] != 'Y' || 'y' )
    Your format specifiers to scanf are not correct in update().
    Code:
    printf("Enter new price: ");
    scanf("%10.2f\n", &inv_list[i].price);
    
    printf("Enter new quantity: ");
    scanf("%8\n", &inv_list[i].quantity);
    Avoid magic numbers like 62, 30, 8. The sizeof operator is useful for this.

    Checking return values is a good thing.

    Code tags are like this: [code] [/code]

    You can disable smilies in your post so that your for(;;) doesn't end up as for(;.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed