Write a C Program to append data into a file using File Handling

By | 01.12.2016

Append data into a file


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 .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

5 1 vote
Article Rating
Category: C Programming File Handling Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments