Computer Programming C: UNIT V: File Operations

File Operations Error Handling in C Programming

It is possible that an error may occur on a file.

ERROR HANDLING

It is possible that an error may occur on a file. Typical error situation are

1. Device over flow.

2. Trying to use a file that has not been opened.

3. Opening a file with an invalid file name.

4. There is enough space on the disc for the file.

5. Attempting to write a write‒protected file.

The global variable errono is set to a specific value meant to designate the type of error, whenever an error occurs in a file I/O function. This value can be used to perform the required exception processing to print the nature of the error the function perror is used. To obtain a char pointer to the error string the function strerror is used. To reset the error indicator of function clearer is used.

The error function reports the statistics of the file indicated. It also takes a file pointer as its argument and returns a non zero integer, if an error has been detected upto that point, during processing otherwise it returns zero

if (ferror (fp)!=0)

       printf ("error occurred \n");

The statement would print the error message, if the reading is not successful.

Example: Program to print the message "Ran out of data" and terminates further reading.

 /* Program to prints the message "Ran out of data" and terminates further reading */

#include <stdio.h>

#include <conio.h>

void main()

{

/* Local definitions */

char *filename;

FILE *fp1, *fp2;

int i,number;

/* Statements */

clrscr();

fpl = fopen("TEST", "W"); /* file open function creates a new file for read/write operation */

for(i= 10;i<=100;i+=10)

putw(i,fpl);

fclose(fp1); /* file close function closes a filé associated with file pointer*/

printf("\nInput filename \n");

open_file:

scanf("%s", filename);

if((fp2 = fopen(filename, "r")) == NULL)

{

    printf("Cannot open the file. \n");

    printf("Type filename again. \n\n");

    goto open file;

} /* if */

else

for(i=1;i<= 20;i++)

{

    number = getw (fp2);

    if (feof(fp2))

    {

        printf("\nRan out of data. \n");

        break; /* break statement comes out of loop */

    } /* if */

    else

        printf("%d\n", number);

    } /* for */

fclose(fp2); /* file close */

} /* main */

OUTPUT

Input filename

TESTS

Cannot open the file.

Type filename again.

TEST

10

20

30

40

50

60

70

80

90

100

Ran out of data.

EXPLANATION: In this program fpl and fp2 are file pointers. The file TEST is opened and assigned to fpl. Then writes the data 10 to 100 using for loop and close the file pointer. The if statement checks the entered file name is correct or not if the entered file name is NULL then prints "cannot open the file", otherwise another if prints the result as "Ran out of data", while the if statement not satisfied then else block is executed and prints the data from the file as shown as output.

Where, perror () is a standard library function which prints the error messages specified by the complier.

Example 2: Program to detect and printf the error message using perror() function.

/* Program to detect and printf the error message using perror() function*/

#include <stdio.h>

#include <conio.h>

#include <process.h>

void main()

{

FILE *fr;

char c, file[] = "lines.txt";

fr=fopen(file, "w");

clrscr();

while(!feof(fr)) /* not end of the file */

{

     c=fgetc(fr);

     if(ferror(fr)) /* ferror() reports error */

     {

          perror(file); /* perror() prints compilers messages */

          exit(1);

     } /* if /*

     else

          printf("%c", c);

     getch();

} /* while

fclose(fr); /* file close function closes a file */

}

OUTPUT

lines.txt: permission denied

 

Computer Programming C: UNIT V: File Operations : Tag: Computer Science : - File Operations Error Handling in C Programming


Computer Programming C: UNIT V: File Operations



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation