Problem Solving using C Language

0 of 77 lessons complete (0%)

File Handling

File Handling in C

In programming, we may require some specific input data to be generated several numbers of times. Sometimes, it is not enough to only display the data on the console. The data to be displayed may be very large, and only a limited amount of data can be displayed on the console, and since the memory is volatile, it is impossible to recover the programmatically generated data again and again. However, if we need to do so, we may store it onto the local file system which is volatile and can be accessed every time. Here, comes the need of file handling in C.

File handling in C enables us to create, update, read, and delete the files stored on the local file system through our C program.

Functions for File Handling

No.FunctionDescription
1fopen()opens new or existing file
2fprintf()write data into the file
3fscanf()reads data from the file
4fputc()writes a character into the file
5fgetc()reads a character from file
6fclose()closes the file
7fseek()sets the file pointer to given position
8fputw()writes an integer to file
9fgetw()reads an integer from file
10ftell()returns current position
11rewind()sets the file pointer to the beginning of the file

Opening File: fopen()

The fopen() function accepts two parameters:

  • The file name (string). If the file is stored at some specific location, then we must mention the path at which the file is stored. For example, a file name can be like “c://some_folder/some_file.ext”.
  • The mode in which the file is to be opened. It is a string.
  • The fopen function works in the following way.
  • Firstly, It searches the file to be opened.
  • Then, it loads the file from the disk and place it into the buffer. The buffer is used to provide efficiency for the read operations.
  • It sets up a character pointer which points to the first character of the file.

Modes of File Opening

ModeDescription
ropens a text file in read mode
wopens a text file in write mode
aopens a text file in append mode
r+opens a text file in read and write mode
w+opens a text file in read and write mode
a+opens a text file in read and write mode
rbopens a binary file in read mode
wbopens a binary file in write mode
abopens a binary file in append mode
rb+opens a binary file in read and write mode
wb+opens a binary file in read and write mode
ab+opens a binary file in read and write mode

Closing File: fclose()

The fclose() function is used to close a file. The file must be closed after performing all the operations on it. The syntax of fclose() function is given below:

int fclose( FILE *fp );  

fprintf

Syntax:
int fprintf(FILE *stream, const char *format [, argument, ...])

Example:
#include <stdio.h>  
main(){  
   FILE *fp;  
   fp = fopen("file.txt", "w");//opening file  
   fprintf(fp, "Hello file by fprintf...\n");//writing data into file  
   fclose(fp);//closing file  
}  

fscanf

Syntax:

int fscanf(FILE *stream, const char *format [, argument, ...])   Example: #include <stdio.h>   main(){      FILE *fp;      char buff[255];//creating char array to store data of file      fp = fopen("file.txt", "r");      while(fscanf(fp, "%s", buff)!=EOF){      printf("%s ", buff );      }      fclose(fp);   }