C Language



Admission Enquiry Form

  

File Handling in C

What is File?

File is a container or a part of memory which is used to store the data permanently.

What is File Handling in C?

FIle handling is technique which is used to perform different operations on the file and manage the file.
C programming language support different operations on file. As we know that an application requires lots of data from secondary memory. An application gets this data with the help of files to performs required operations on this data. File is well known part of computer world. So, File is a container which is used to store the data permanently.






File Operations in C

  1. Opening a file
  2. Writing or reading or append of file
  3. Closing a file

1. Opening a File

Opening of a file means open a file before performing any task on it such as writing, reading and appending (updating). Usually, opening a file establishes link between the operating system(OS) and file. With this, both file name and mode are inform to OS to open a file and perform some specific operation on it.
To open a file, we use ‘FILE’ structure which is already existed and defined in header file stdio.h . So, this header file must be included in the program for performing file operation.

Steps to perform opening a file:

1. The file pointer of FILE structure is declared to open a file.Like: FILE *fptr; Here , fptr is a file pointer. Each file has its own file structure.

2. fopen() which is library function is used to perform opening of a file. It takes two arguments. The first argument shows name of the file and second indicates mode of a file such as ‘w’ for write, ‘r’ for read and ‘a’ for append. fopen() use given below: fptr= fopen(“FILE_NAME”,”MODE”); Here, fptr is file pointer. As we know that pointer is a special vriable which holds address of another variable but of same type. Here, file pointer fptr is used to hold the address of file which is opened by fopen().




There are different types of mode:

Mode Description
r opens a text file in read mode
w opens a text file in write mode
a opens a text file in append mode
r+ opens a text file in both read and write mode
w+ opens a text file in both read and write mode
a+ opens a text file in both read and write mode
rb opens a binary file in read mode
wb opens or create a binary file in write mode
ab opens a binary file in append mode
rb+ opens a binary file in both read and write mode
wb+ opens a binary file in both read and write mode
ab+ opens a binary file in both read and write mode



Functions List of C File Handling

C provides a number of functions that helps to perform basic operatios in file. They are as follows:

Function Name Description
fopen() create a new file or open an existing file
fclose() closes a file
fprintf() writes a set of data to a file
fscanf() reads a set of data from a file
getc() reads a character from a file
putc() writes a character to a file
fgets() reads a string from a file
fputs() writes a string to a file
getw() reads a integer from a file
putw() writes a integer to a file
fseek() set the position of pointer to desire point in a file
ftell() gives the current position of pointer in a file
rewind() set the position of pointer in the beginning of a file




Mode of a File

  1. Write Mode
  2. Read Mode
  3. Append Mode

If you want to learn these modes then visit the next page