Write a C Program to appending data in file
Program:
#include<stdio.h>
#include<conio.h>
struct invent_record
{
char name[10];
int number;
float price;
int quantity;
};
int main()
{
struct invent_record item;
char filename[10];
int response;
FILE *fp;
long n;
void append (struct invent_record *x, 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);
fseek(fp,0,SEEK_END);
n=ftell(fp);
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:
Type filename:Shivam
Item name:Soap
Item number:2
Item price:40
Quantity:5
Item Soap appended.
Do you want to add another item (1 for YES /0 for NO)?1
Item name:color
Item number:3
Item price:5
Quantity:12
Item color appended.
Do you want to add another item (1 for YES /0 for NO)?0
Soap 2 40.00 5
color 3 5.00 12
--------------------------------
Process exited after 135.8 seconds with return value 0
Press any key to continue . . .
Program:
#include<stdio.h>
#include<conio.h>
struct invent_record
{
char name[10];
int number;
float price;
int quantity;
};
int main()
{
struct invent_record item;
char filename[10];
int response;
FILE *fp;
long n;
void append (struct invent_record *x, 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);
fseek(fp,0,SEEK_END);
n=ftell(fp);
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:
Type filename:Shivam
Item name:Soap
Item number:2
Item price:40
Quantity:5
Item Soap appended.
Do you want to add another item (1 for YES /0 for NO)?1
Item name:color
Item number:3
Item price:5
Quantity:12
Item color appended.
Do you want to add another item (1 for YES /0 for NO)?0
Soap 2 40.00 5
color 3 5.00 12
--------------------------------
Process exited after 135.8 seconds with return value 0
Press any key to continue . . .
0 Comments:
Post a Comment