A File is a collection of data stored on a secondary storage device like hard disk. File operation is
to combine all the input data into a file and then to operate through the C program. Various
operations like insertion, deletion, opening closing etc can be done upon a file. When the program
is terminated, the entire data is lost in C programming.

If you want to keep large volume of data, it is time consuming to enter the entire data. But, if file is created these information can be accessed using few commands.

There are large numbers of functions to handle file I/O in C language. In this tutorial, you will
learn to handle standard I/O(High level file I/O functions) in C. High level file I/O functions can
be categorized as:
1. Text file
2. Binary file

A file can be open in several modes for these operations. The various modes are:


fopen and freopen opens the file whose name is in the string pointed to by filename and associates a stream with it. Both return a pointer to the object controlling the stream, or if the open operation fails a null pointer.

The error and end-of-file(EOF) indicators are cleared, and if the open operation fails error is set. freopen differs from fopen in that the file pointed to by stream is closed first when already open and any close errors are ignored.

Q1. Write a program to open a file using fopen().

#include void
file *fp;
fp=fopen(“student.DAT”, “r”);
printf(“The file could not be open”);

Q2. Write a C program to read name and marks of n number of students from user and store them
in a file. If the file previously exits, add the information of n students.

int main()
char name[50];
int marks, i,n;
printf(“Enter number of students”);
scanf(“%d”, &n);
FILE *fptr;
if (fptr==NULL){ printf("Error!");
{ printf("For student%d\nEnter name: ",i+1);
printf(“Enter marks”);
scanf(“%d”, &marks);
fprintf(fptr, “\nName: %s\nMarks=%d\n”, name, marks);
} fclose(fptr);
Return 0;

The fclose function causes the stream pointed to be flushed and the associated file to be closed.
Any unwritten buffered data for the stream are delivered to the host environment to be written to
the file; any unread buffered data are discarded. The stream is disassociated from the file. If the
associated buffer was automatically allocated, it is deallocated. The function returns zero if the
stream was successfully closed or EOF if any errors were detected.

Q.3. Write a program to read data from file and close using fclose function.

int main()
int n
FILE *fptr;
if ((fptr=fopen("C:\\program.txt","r"))==NULL){
printf("Error! opening file");
exit(1); // Program exits if file pointer returns NULL.
printf("Value of n=%d",n);
return 0;

Q4. Write a C program to write all the members of an array of strcures to a file using fwrite().
Read the array from the file and display on the screen.

Struct s
Char name[50];
Int height;
Int main()
Struct s a[5], b[5];
FILE *fptr;
Int I;
Fptr=fopen(“file.txt”, “wb”);
For(i=0; i<5; ++i)
printf("Enter name: ") ;
printf("Enter height: ");
printf("Name: %s\nHeight: %d",b[i].name,b[i].height);
} fclose(fptr);

Try Now – Programming In C MCQs