Program to appending data in file

Advertisements

Prev Tutorial Next Tutorial

Appending data in file

Appending data in file in C

#include<stdio.h>
#include<conio.h>

struct invent_record
{
char   name[10];
int    number;
float  price;
int    quantity;
};

void main()
{
struct invent_record item;
char  filename[10];
int   response;
FILE  *fp;
long  n;
void append (struct invent_record *x, FILE *y);
clrscr();
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);

fseek(fp,0,SEEK_END); /*Set the file pointer at the end of file*/
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);
getch();
}
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

append data in file
Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements