Append mode in file by R4R Team

Append mode:
It is similar as the written mode in file Handling.
-All type of operation to store data in file are same as write mode.

Difference between append and write :
-Basically, when we use the write mode in the file and write something in existing file then old data will erase and new data takes place.
-But in Append mode, old data will not erase, and new data are also store in file.

To Append in file what things we require ?
-A file pointer.
-fopen() function to open file in append mode.
-fprintf() to write in file.
-fclose() to close the file.

Open file in Append mode:
Syntax-
fp=fopen("Filename","a");


fprintf():
-This function are write the text content inside the file.
Syntax-
fprintf(fp,"Mydata");


program-

#include< stdio.h>
void main()
{
FILE *fp;
char data[50];
//open file in write mode
fp=fopen("myfile.txt","a");
printf("Enter Data\n");
scanf("%s",&data);
printf("Now we put new data inside the file\n");
fprintf(fp,data);
fclose(fp);// close file
printf("Your file is safely closed now");
}


output-

Enter Data
This is my new data which i store in file
Now we put data inside the file
Your file is safely closed now.




Leave a Comment: