Explain C Error handling functions



File is collection of records or is a place on hard disk, where data is stored permanently.

Operations on files

The operations on files in C programming language are as follows −

  • Naming the file
  • Opening the file
  • Reading from the file
  • Writing into the file
  • Closing the file

Syntax

The syntax for opening a file is as follows −

FILE *File pointer;

For example, FILE * fptr;

The syntax for naming a file is as follows −

File pointer = fopen ("File name", "mode");

For example,

fptr = fopen ("sample.txt", "r"); FILE *fp; fp = fopen ("sample.txt", "w");

Error Handling in Files

Some of the errors in files are as follows −

  • Trying to read beyond an end of file.
  • Device over flow.
  • Trying to open an invalid file.
  • Performing an invalid operation by opening a file in a different mode.

ferror( )

It is used for detecting an error while performing read / write operations.

The syntax for ferror() function is as follows −

Syntax

int ferror (file pointer);

For example,

Example

FILE *fp; if (ferror (fp)) printf ("error has occurred");

It returns zero, if success and a non- zero, otherwise.

Program

Following is the C program for using ferror() function −

#include<stdio.h> int main(){    FILE *fptr;    fptr = fopen("sample.txt","r");    if(ferror(fptr)!=0)       printf("error occurred");    putc('T',fptr);    if(ferror(fptr)!=0)       printf("error occurred");    fclose(fptr);    return 0; }

Output

When the above program is executed, it produces the following result −

error occurred Note: try to write a file in the read mode results an error.

perror ( )

It is used for printing an error.

The syntax for perror() function is as follows −

Syntax

perror (string variable);

For example,

Example

FILE *fp; char str[30] = "Error is"; perror (str);

The output is as follows −

Error is: error 0

Program

Following is the C program for using perror() function −

#include<stdio.h> int main ( ){    FILE *fp;    char str[30] = "error is";    int i = 20;    fp = fopen ("sample.txt", "r");    if (fp == NULL){       printf ("file doesnot exist");    }    else{       fprintf (fp, "%d", i);       if (ferror (fp)){          perror (str);          printf ("error since file is opened for reading only");       }    }    fclose (fp);    return 0; }

Output

When the above program is executed, it produces the following result −

error is: Bad file descriptor error since file is opened for reading only

feof( )

It is used for checking whether an end of the file has been reached (or) not.

The syntax for feof() function is as follows −

Syntax

int feof (file pointer);

For example,

Example

FILE *fp; if (feof (fp)) printf ("reached end of the file");

It returns a non-zero, if success and zero, otherwise.

Program

Following is the C program for using feof() function −

 Live Demo

#include<stdio.h> main ( ){    FILE *fp;    int i,n;    fp = fopen ("number. txt", "w");    for (i=0; i<=100;i= i+10){       putw (i, fp);    }    fclose (fp);    fp = fopen ("number. txt", "r");    printf ("file content is");    for (i=0; i<=100; i++){       n = getw (fp);       if (feof (fp)){          printf ("reached end of file");          break;       }else{          printf ("%d", n);       }    }    fclose (fp);    getch ( ); }

Output

When the above program is executed, it produces the following result −

File content is 10 20 30 40 50 60 70 80 90 100 Reached end of the file.
Updated on: 2021-03-24T13:53:46+05:30

876 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements