Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

APPENDING ITEMS TO AN EXISTING FILE USING DO WHILE LOOP

Posted By: Rald Fischer     Category: C Programming     Views: 5922

Write a program to append additional items to the file INVENTORY and print the total contents of the file.

Code for APPENDING ITEMS TO AN EXISTING FILE USING DO WHILE LOOP in C Programming

#include  <stdio.h>                                              
                                                                    
   struct invent_record                                             
   {
       char   name[10];                                             
       int    number;                                               
       float  price;                                                
       int    quantity;                                             
   };                                                               
                                                                    
   main()                                                           
   {
       struct invent_record item;                                   
       char  filename[10];                                          
       int   response;                                              
       FILE  *fp;                                                   
       long  n;                                                     
       void append (struct invent_record 8x, file *y);                                                             
       printf("Type filename:");                                    
       scanf("%s", filename);                                       
                                                                    
       fp = fopen(filename, "a+");                                  
       do                                                           
       {
          append(&item, fp);                                        
          printf("\nItem %s appended.\n",item.name);                
          printf("\nDo you want to add another item\                
           (1 for YES /0 for NO)?");                                        
          scanf("%d", &response);                                   
       }  while (response == 1);                                    
                                                                    
       n = ftell(fp);      /* Position of last character  */
fclose(fp); fp = fopen(filename, "r"); while(ftell(fp) < n) { fscanf(fp,"%s %d %f %d", item.name, &item.number, &item.price, &item.quantity); fprintf(stdout,"%-8s %7d %8.2f %8d\n", item.name, item.number, item.price, item.quantity); } fclose(fp); } void append(struct invent_record *product, File *ptr) { printf("Item name:"); scanf("%s", product->name); printf("Item number:"); scanf("%d", &product->number); printf("Item price:"); scanf("%f", &product->price); printf("Quantity:"); scanf("%d", &product->quantity); fprintf(ptr, "%s %d %.2f %d", product->name, product->number, product->price, product->quantity); } Output Type filename:INVENTORY Item name:XXX Item number:444 Item price:40.50 Quantity:34 Item XXX appended. Do you want to add another item(1 for YES /0 for NO)?1 Item name:YYY Item number:555 Item price:50.50 Quantity:45 Item YYY appended. Do you want to add another item(1 for YES /0 for NO)?0 AAA-1 111 17.50 115 BBB-2 125 36.00 75 C-3 247 31.75 104 XXX 444 40.50 34 YYY 555 50.50 45
  
Share: 


Didn't find what you were looking for? Find more on APPENDING ITEMS TO AN EXISTING FILE USING DO WHILE LOOP Or get search suggestion and latest updates.

Rald Fischer
Rald Fischer author of APPENDING ITEMS TO AN EXISTING FILE USING DO WHILE LOOP is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!