Explain C Error handling functions

In C programming, error handling functions are essential for managing errors that occur during file operations. These functions help detect and report errors when reading from or writing to files, ensuring robust and reliable programs.

Syntax

int ferror(FILE *stream);
void perror(const char *s);
int feof(FILE *stream);

Common File Operation Errors

Some common errors in file operations include −

  • Trying to read beyond the end of file
  • Device overflow
  • Trying to open an invalid file
  • Performing invalid operations (e.g., writing to a read-only file)

ferror() Function

The ferror() function is used to detect errors while performing read/write operations. It returns zero if no error has occurred, and a non-zero value if an error has occurred.

Example

#include <stdio.h>

int main() {
    FILE *fptr;
    fptr = fopen("sample.txt", "r");
    
    if (fptr == NULL) {
        printf("Error opening file<br>");
        return 1;
    }
    
    if (ferror(fptr) != 0) {
        printf("Error occurred while opening<br>");
    }
    
    /* Trying to write to a file opened in read mode */
    putc('T', fptr);
    
    if (ferror(fptr) != 0) {
        printf("Error occurred while writing<br>");
    }
    
    fclose(fptr);
    return 0;
}
Error occurred while writing

perror() Function

The perror() function prints a descriptive error message to stderr. It takes a string parameter that is printed followed by a colon and the system error message.

Example

#include <stdio.h>

int main() {
    FILE *fp;
    char str[30] = "File error";
    int i = 20;
    
    fp = fopen("sample.txt", "r");
    
    if (fp == NULL) {
        perror("File opening failed");
        return 1;
    } else {
        /* Trying to write to read-only file */
        fprintf(fp, "%d", i);
        
        if (ferror(fp)) {
            perror(str);
            printf("Error: file is opened for reading only<br>");
        }
    }
    
    fclose(fp);
    return 0;
}
File error: Bad file descriptor
Error: file is opened for reading only

feof() Function

The feof() function checks whether the end of file has been reached. It returns a non-zero value if the end of file is reached, and zero otherwise.

Example

#include <stdio.h>

int main() {
    FILE *fp;
    int i, n;
    
    /* Write numbers to file */
    fp = fopen("numbers.txt", "w");
    if (fp == NULL) {
        printf("Error creating file<br>");
        return 1;
    }
    
    for (i = 0; i <= 100; i += 10) {
        fprintf(fp, "%d ", i);
    }
    fclose(fp);
    
    /* Read numbers from file */
    fp = fopen("numbers.txt", "r");
    if (fp == NULL) {
        printf("Error opening file<br>");
        return 1;
    }
    
    printf("File content: ");
    while (fscanf(fp, "%d", &n) == 1) {
        printf("%d ", n);
        
        if (feof(fp)) {
            printf("\nReached end of file<br>");
            break;
        }
    }
    
    fclose(fp);
    return 0;
}
File content: 0 10 20 30 40 50 60 70 80 90 100 
Reached end of file

Key Points

  • ferror() detects errors during file operations
  • perror() provides descriptive error messages
  • feof() checks for end-of-file condition
  • Always check for NULL when opening files
  • These functions work together to provide comprehensive error handling

Conclusion

Error handling functions in C are crucial for writing robust file handling programs. Using ferror(), perror(), and feof() together provides comprehensive error detection and reporting capabilities for file operations.

Updated on: 2026-03-15T13:53:40+05:30

982 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements