File Handling in C


  • File is a set of records which can be any number, symbol or text stored on the disk.
  • In C language Files stored in memory are handled by the several library functions.
  • In C language we can categorized file operation in three parts:
    • Opening a file
    • Closing file
    • Reading or writing file

Before discussing about these file operations let us go through the various file handling function that are available in standard library. These functions are declared below in the given table:

file handling

Opening a file

  • A file has to be opened before performing any read or write operation on it.
  • fopen() function is used to open a file.
  • fopen() function is basically open a existing file or create a new file.
  • Here is a syntax for opening the file:
  • FILE *fp;

    fp=fopen( const char *filename, const char * mode);
  • In the above syntax fp is a pointer type variable that contains the address of the FILE that has been defined in the header file "stdio.h". filename is a character constant that indicates the name of the file and mode is also a character constant that represent the access mode of the file. Access modes can be categorized into two parts:1) Text modes 2) Binary Modes
45, -16, +56, 67 etc.

Text modes

  • Here is the list of text mode in which a file can be opened:
  • file handling

    Binary modes

  • Here is the list of text mode in which a file can be opened:
  • file handling

Closing a file

  • The file that is opened with the fopen() function should be closed after reading and writing operations are over.
  • fclose() function is used to close a file.
  • The basic syntax of the fclose function is as follows:.
  • fclose( File *fp);
  • This function close the file associated with the file pointer fp. This function closes one file at a time. To close all the opened file we can use fcloseall() function.

Writing a file

  • We can write contents in file by using fprintf() function.
  • This function is used for writing characters, strings, integers, floats etc. to the file.
  • The basic syntax of the fprintf() function is as follows:
  • int fprintf(FILE *fp,const char *format, ...)
  • Here is a program that open a file and write some text using fprintf() function:
Example
#include<stdio.h>

#include<conio.h>

int main()

{

FILE *fp;

char data[15];

fp=fopen("data.txt","w");

printf("Enter data:");

gets(data);

fprintf(fp,"%s",data);

fclose(fp);

getch();

return 0;

}

  • The output of the above program will be as follows:
  • Output
    Enter data: Made in India

    In the above program fprintf() function writes the string to the file pointed by pointer fp.We can also use putc(),fputc(), and fputs() function for writing data into file.

    Writing a file

    • We can read contents in file by using fprintf() function.
    • This function is used for reading characters, strings, integers, floats etc. from the file pointed by file pointer.
    • The basic syntax of the fprintf() function is as follows:
    • int fscanf(FILE *fp, const char *format, ..........)
    • Here is a program that open a file and write some text using fscanf() function:
    Example
    #include<stdio.h>

    #include<conio.h>

    int main()

    {

    FILE *fp;

    int value;

    fp=fopen("data.txt","w+");

    printf("Enter value:");

    scanf("%d",&value);

    fprintf(fp,"%d",value);

    printf("entered value is:");

    fscanf(fp,"%d",value);

    printf("%d",value);

    fclose(fp);

    getch();

    return 0;

    }

  • The output of the above program will be as follows:
  • Output
    Enter value:2

    entered value is:2

    In the above program fscanf() function reads the data from the file named "data.txt".We can also use fgetc(), getc() and fgets() functions to read data from the file.