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

We are the best Embedded Training Institutes in Bangalore with 100% job placement record. Our top most priority is to put more and more focus on practical aspects of the Training. We believe if the student is good with practical knowledge he can crack any interview he/she wants. We also promote the meritorious students and those who are in need but can't afford the expensive training in the other institutes are also welcomed at Professional Institute.

It is needless to say that there are many institute which provides Embedded Training in Bangalore but most of them focus on the quantity of the course but we focus majorly on the quality of the education. The reason is very simple, those institute have profit as their main motive but we are mostly concerned with the future of the students. That's why we charge very minimal fees from students.

PTInstitute © 2017 All rights reserved.