Write a C Program to append data into a file using File Handling. Here’s simple Program to append data into a file using File Handling in C Programming Language.
Below is the source code for C Program to append data into a file using File Handling which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C Program to append data into a file using File Handling */
#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,n2;
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); /*Set the file pointer at the end of file*/
n=ftell(fp); /* Position of last character */
fclose(fp);
return 0;
}
void append(struct invent_record *product, FILE *ptr)
{
printf("\nItem 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 : :
/* C Program to append data into a file using File Handling */
Type filename:C:\\Users\\acer\\Documents\\file4.txt
Item name:CodezClub
Item number:1
Item price:2000
Quantity:20
Item CodezClub appended.
Do you want to add another item (1 for YES /0 for NO)?1
Item name:Programming
Item number:2
Item price:3000
Quantity:70
Item Programming appended.
Do you want to add another item (1 for YES /0 for NO)?0
Process returned 0
Above is the source code for C Program to append data into a file using File Handling which is successfully compiled and run on Windows System.The Output of the program is shown above .
Below is the source code for C Program to append data into a file using File Handling which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
OUTPUT : :
Above is the source code for C Program to append data into a file using File Handling which is successfully compiled and run on Windows System.The Output of the program is shown above .
need an explanation for this answer? contact us directly to get an explanation for this answer