The file handling in C can be broadly divided into two types

  1. High level
  2. low level
  • High level file handling is managed by library functions while low level file handling is done by system calls.
  • The high level file handling is commonly used since it is easier to manage and hides most of the details from the programmer.

How data are getting store in a file:

There are two ways of storing data in files, Binary format and Text format.

  1. In text format, data is stored as lines of characters where each line terminated with a new line("\n"). Text files are in human readable form & they can be created & read using any text editor.
  2. In the binary format data are stored on the same way as it is represented in the computer memory.(Unix based systems does not make any distinction between binary file and text file).Binary files are not in the human readable form ,they can be created & read by specific programs only.

Steps For File Operation:

The steps for file operation in C Programming are:

  1. Open A file
  2. Read the file or write data in the file
  3. Close the file.

Opening a File:

A file must be opened before doing any I/O operations on that file.This will establish a connection between the program & the file.

  • A structure named FILE which is defined in side stdio.h contains all the information about the file like name,status,buffer size,current position,EOF (End Of File) status etc.All these information are hidden from the programmer & operating system takes care about this.
  • A file pointer is a pointer to a structure of type FILE.When ever a file is opened a structure of type FILE associated with it and a file pointer that points to this structure identifies this file.
  • Syntax:  FILE *fopen(const char *filename,const char *mode);
  • It takes two things as argument  name of the file to be opened & mode that decides which operations (read,write,append etc).
  • on success fopen() returns a pointer of file type that points to first character of the file and on failure it returns NULL.

Different modes in which file can be opened are,

  1. "w"(write mode)-If file does not present it creates a new file & opens it. If file is already present it will erased the previous contains &  open the file.
  2. "r"(read mode)-If file exists it will open the file without erasing contains of the file.
  3. "a"(append mode)-If file exists it will open the file & will add data in the file. If file does not exits then it will create new file & will add data in it.
  4. "w+"(Write+read)-works same way as write mode.
  5. "r+"(read+write)-works same way as read but we can write & modify existing data.
  6. "a+"(append+read)-works same as append mode also we can read data.

Errors in Opening a File:

Errors in Opening a File May Occur Due To Various Reasons some are

  1. if we try to open a file in read or update mode i.e "r+" mode and the file does not exist or we don't have read permission for the file.
  2. If we try to create a file & there is no disk space available or we don't have write permission .
  3. If we try to create a file that already exists and we don't have permission to delete that file.
  4. operating system limits the number of files that can be opened at a time and we are trying to open more files than that number.
  5. If a file already available & we opened it in "w" mode then previous data will get erased without showing any error.
  6. we should not use single quotes for the mode because it is a string not character. "w"-valid 'w'-invalid.

Functions Used To Read Data From File/Write Data In The File: 

  • Functions such as fputc(),putw(),fputs() etc are used to write data in a file.
  • Similarly fgetc(),getw(),fgets() atc are used to read data from the file.

Closing a File:

The files that are opened using fopen()  must be closed when no more operations are to be performed. fclose() function is used to close  a file.

syntax:fclose(FILE  *fp);

After closing the file the connection between file & program is broken.

End Of File:

While a function reading file it should know where the file ends so that it can stop reading there.When the EOF is reached the operating system sends an EOF signal to the program .EOF is defined inside stdio.h & its value is -1.

Write a program to display the total number of alphabets & numeric characters in a file.

let a file text.txt is present in "c:\cprog\file\text.txt" path.The file contains can be seen in below screenshot.

In the file text.txt we have to count the total number of alphabets are present & total number of numeric characters are present.


#include <stdio.h>
#include<stdlib.h>
void main()
{
   FILE *fp; //File pointers
   int n1,n2;
   char ch;
   n1=n2=0;
   fp=fopen("c:\cprog\file\text.txt","r");//opening file in read mode
   if(fp==NULL)
   {
      printf("File not available");
      exit(1);
   }
   while((ch=fgetc(fp))!=EOF)
   {
      if(((ch>='a')&&(ch<='z'))||((ch>='A')&&(ch<='Z')))
      {
         n1++;
      }
      else
      {
         if((ch>='0'&&ch<='9'))
         {
            n2++;
         }
      }
   }
   printf("No of alphabets are=%d\n",n1);
   printf("No of numeric charecters are=%d\n",n2);
}

Output:

 

Have Fun With C-Programming! ;)

Comments