Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

HANDLING OF FILES WITH MIXED DATA TYPES (fscanf and fprinf)

Posted By: Sam Evans     Category: C Programming     Views: 4091

HANDLING OF FILES WITH MIXED DATA TYPES
(fscanf and fprinf).

Code for HANDLING OF FILES WITH MIXED DATA TYPES (fscanf and fprinf) in C Programming

#include  <stdio.h>                                              
                                                                    
   main()                                                           
   {
       FILE  *fp;                                                   
       int    number, quantity, i;                                  
       float  price, value;                                         
       char   item[10], filename[10];                               
                                                                    
       printf("Input file name\n");                                 
       scanf("%s", filename);                                       
       fp = fopen(filename, "w");                                   
       printf("Input inventory data\n\n");                          
       printf("Item name  Number   Price   Quantity\n");            
       for(i = 1; i <= 3; i++)                                      
       {
          fscanf(stdin, "%s %d %f %d",                              
                        item, &number, &price, &quantity);          
          fprintf(fp, "%s %d %.2f %d",                              
                        item, number, price, quantity);             
       }                                                            
       fclose(fp);                                                  
       fprintf(stdout, "\n\n");                                     
                                                                    
       fp = fopen(filename, "r");                                   
                                                                    
       printf("Item name  Number   Price   Quantity    Value\n");   
       for(i = 1; i <= 3; i++)                                      
       {
          fscanf(fp, "%s %d %f d",item,&number,&price,&quantity);  
          value = price * quantity;                                 
          fprintf(stdout, "%-8s %7d %8.2f %8d %11.2f\n",            
                         item, number, price, quantity, value);     
       }                                                            
       fclose(fp);                                                  
   }                                                                

Output                                                           
                                                                    
   Input file name                                                  
   INVENTORY                                                        
   Input inventory data                                             
                                                                    
   Item name  Number   Price   Quantity                             
   AAA-1  111  17.50  115                                           
   BBB-2  125  36.00  75                                            
   C-3    247  31.75  104                                           
                                                                    
   Item name  Number   Price   Quantity    Value                    
   AAA-1        111    17.50      115     2012.50                   
   BBB-2        125    36.00       75     2700.00                   
   C-3          247    31.75      104     3302.00                   
  
Share: 



Sam Evans
Sam Evans author of HANDLING OF FILES WITH MIXED DATA TYPES (fscanf and fprinf) is from London, United Kingdom.
 
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!