Loading content...
Loading content...
File operations are an important part of C programming. While working with files, various errors can occur such as missing files, insufficient permissions, invalid file pointers, or disk-related issues. Proper error handling helps prevent program crashes and ensures reliable execution.
Without proper error handling:
Programs may crash unexpectedly.
Incorrect data may be processed.
Resources such as files may remain open.
Users may receive incorrect output.
Good error handling makes applications more stable and easier to debug.
Some common errors that occur during file handling are:
Occurs when attempting to open a file that does not exist.
Occurs when the program does not have sufficient permissions to access a file.
Occurs when there is no available storage space for writing data.
Occurs when trying to create a file that already exists.
Occurs when file operations are performed using a NULL file pointer.
Occurs when reading reaches the end of a file.
Occurs when a file was not opened successfully before performing operations.
Occurs when fclose() fails to close a file properly.
When opening a file in read mode, fopen() returns NULL if the file does not exist.
plaintext
#include <stdio.h>
int main()
{
FILE *file = fopen("file.txt", "r");
if (file == NULL)
{
perror("Error");
return 1;
}
fclose(file);
return 0;
}plaintext
Error: No such file or directoryfopen() tries to open the file.
If the file is not found, it returns NULL.
perror() displays the system-generated error message.
If the program lacks permission to access a file, fopen() fails.
plaintext
#include <stdio.h>
int main()
{
FILE *file = fopen("/restricted/file.txt", "w");
if (file == NULL)
{
perror("Permission Denied");
}
return 0;
}plaintext
Permission Denied: Permission deniedErrors during writing can be detected using ferror().
plaintext
#include <stdio.h>
int main()
{
FILE *file = fopen("data.txt", "w");
if (file == NULL)
{
perror("Error opening file");
return 1;
}
fprintf(file, "Writing data");
if (ferror(file))
{
perror("Error writing file");
}
fclose(file);
return 0;
}ferror() checks whether an error occurred during file operations.
Useful for detecting write failures such as a full disk.
To prevent overwriting an existing file, use "wx" mode.
plaintext
#include <stdio.h>
#include <errno.h>
int main()
{
FILE *file = fopen("test.txt", "wx");
if (file == NULL)
{
if (errno == EEXIST)
{
printf("File already exists");
}
return 0;
}
fprintf(file, "New File");
fclose(file);
return 0;
}plaintext
File already existsAlways verify that the file pointer is valid before using it.
plaintext
#include <stdio.h>
int main()
{
FILE *file = NULL;
if (file == NULL)
{
printf("Invalid file pointer\n");
}
return 0;
}plaintext
Invalid file pointerUse feof() to determine whether the end of a file has been reached.
plaintext
#include <stdio.h>
int main()
{
FILE *file = fopen("test.txt", "r");
char ch;
while ((ch = fgetc(file)) != EOF)
{
putchar(ch);
}
if (feof(file))
{
printf("\nEnd of file reached");
}
fclose(file);
return 0;
}plaintext
End of file reachedfgetc() reads characters one by one.
feof() confirms whether EOF was reached.
Always verify that the file was opened successfully.
plaintext
#include <stdio.h>
int main()
{
FILE *file = fopen("example.txt", "r");
if (file == NULL)
{
printf("File could not be opened\n");
}
else
{
printf("File opened successfully\n");
fclose(file);
}
return 0;
}plaintext
File could not be openedThe fclose() function returns:
0 on success
EOF (-1) on failure
plaintext
#include <stdio.h>
int main()
{
FILE *file = fopen("test.txt", "w");
fprintf(file, "Hello World");
if (fclose(file) == EOF)
{
printf("File closing error");
}
else
{
printf("File closed successfully");
}
return 0;
}plaintext
File closed successfullyPrints a descriptive error message.
plaintext
perror("Error");Checks if an error occurred during file operations.
plaintext
if(ferror(file))
{
printf("File Error");
}Checks if the end of file has been reached.
plaintext
if(feof(file))
{
printf("EOF Reached");
}Stores error codes generated by system calls and library functions.
plaintext
#include <errno.h>Always check the return value of fopen().
Verify write operations using ferror().
Use feof() when reading files.
Always close files using fclose().
Use perror() for detailed error messages.
Check for NULL before using a file pointer.
File operations may fail due to missing files, permissions, invalid pointers, disk issues, or EOF conditions.
fopen() should always be checked for NULL.
perror() provides system error messages.
ferror() detects file operation errors.
feof() detects end-of-file conditions.
fclose() should always be checked for successful file closure.
Proper error handling makes programs reliable, secure, and easier to maintain.