C program to opening, writing, and closing a file using file operation

Write a program to open a file using file operations and then add new data to the existing file and finally close it?

In this tutorial, you will learn how to Write a C program to open a file then add new data to the existing file and finally close it.
What is a File in C language?
A file represents a sequence of bytes on the disk where a group of related data is stored. A file is created for permanent storage of data. It is a readymade structure.

The function “fopen” opens a file and associates a stream with that opened file. You need to specify the method of opening the file and the filename as arguments.
After a disk file is read, written, or appended with some new data, you have to disassociate the file from the specified stream. This is done by calling the “fclose” function:

C Programming


int main()
char data[50]={“Hello I am kevin”};
char *dptr1,*dptr2,*dptr3;

FILE *ftr;
size_t s,n;

ftr = fopen(“hel.txt”,”w+”);

if (ftr==0)
printf(“File open failed \n”);

printf(“file open successfully\n”);

n=fwrite (data,10, 1,ftr);

printf(“hi my fwrite fun had written %ld bytes\n”,n);

fclose (ftr);

return 0;


Leave a comment