File Handling in C


Prev Tutorial Next Tutorial

File Handling in C

File Handling concept in C language is used for store a data permanently in computer. Using this concept we can store our data in Secondary memory (Hard disk). All files related function are available in stdio.h header file.

How to achieve File Handling in C

For achieving file handling in C we need follow following steps

  • Naming a file
  • Opening a file
  • Reading data from file
  • Writing data into file
  • Closing a file

Functions use in File Handling in C

1fopen()To create a file
2fclose()To close an existing file
3getc()Read a character from a file
4putc()Write a character in file
5fprintf()To write set of data in file
6fscanf()To read set of data from file.
5getw()To read an integer from a file
6putw()To write an integer in file

Defining and Opening a File

Data structure of file is defined as FILE in the standard I/O function. So all files should be declared as type FILE.

Before opening any file we need to specify for which purpose we open file, for example file open for write or read purpose.


FILE *fp;
pf=fopen("filename", "mode");

Here fp declare a variable as a pointer to the data type FILE.

Closing a File

A file must be close after completion of all operation related to file. For closing file we need fclose() function.



File Opening mode

1rReadingOpen the file for reading only.
2wWritingOpen the file for writing only.
3aAppendingOpen the file for appending (or adding) data to it.
4r+Reading + WritingNew data is written at the beginning override existing data.
5w+Writing + ReadingOverride existing data.
6a+Reading + AppendingTo new data is appended at the end of file.

Input/Output Operation on files

To perform Input/Output Operation on files we need below functions.

1getc()Read a character from a filegetc( fp)
2putc()Write a character in fileputc(c, fp)
3fprintf()To write set of data in filefprintf(fp, "control string", list)
4fscanf()To read set of data from file.fscanf(fp, "control string", list)
5getw()To read an integer from a file.getw(fp)
6putw()To write an integer in file.putw(integer, fp)

Write data in File



void main()
FILE *fp;
char ch[20];
fp=fopen("hh.txt", "w");
printf("Enter any Text: ");
scanf("%s",&ch); // Read data from keyboard
fprintf(fp,"%s",ch); // Write data in file

Prev Tutorial Next Tutorial


Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: or 9999595223

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements

Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: or 9999595223 Try this Keyword C++ Programs