Introduction
ferror()
function in C is a standard library function that checks the error indicator for the specified stream. It is part of the C standard library (stdio.h
) and is commonly used to detect errors during input and output operations.ferror() Function Syntax
The syntax for the ferror()
function is as follows:
int ferror(FILE *stream);
Parameters:
stream
: A pointer to aFILE
object that specifies the stream to be checked.
Returns:
- The function returns a non-zero value if the error indicator is set for the specified stream. Otherwise, it returns
0
.
Examples
Checking for Errors After Reading a File
To demonstrate how to use ferror()
to check for errors after reading a file, we will write a simple program.
Example
#include <stdio.h>
int main() {
FILE *file;
int ch;
// Open the file for reading
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error: Could not open file for reading.\n");
return 1;
}
// Read characters until the end of the file
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
// Check if an error occurred
if (ferror(file)) {
printf("\nError occurred while reading the file.\n");
} else if (feof(file)) {
printf("\nEnd of file reached.\n");
}
// Close the file
fclose(file);
return 0;
}
Output (assuming example.txt
contains the text "Hello, World!"):
Hello, World!
End of file reached.
Checking for Errors After Writing to a File
This example shows how to use ferror()
to check for errors after writing to a file.
Example
#include <stdio.h>
int main() {
FILE *file;
const char *text = "Hello, World!";
// Open the file for writing
file = fopen("output.txt", "w");
if (file == NULL) {
printf("Error: Could not open file for writing.\n");
return 1;
}
// Write text to the file
if (fputs(text, file) == EOF) {
printf("Error occurred while writing to the file.\n");
}
// Check if an error occurred
if (ferror(file)) {
printf("Error occurred while writing to the file.\n");
} else {
printf("Writing to the file was successful.\n");
}
// Close the file
fclose(file);
return 0;
}
Output:
Writing to the file was successful.
Real-World Use Case
Reading Data from a File with Error Handling
In real-world applications, the ferror()
function can be used to handle errors gracefully while reading data from a file.
Example
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main() {
FILE *file;
char buffer[256];
// Open the file for reading
file = fopen("data.txt", "r");
if (file == NULL) {
printf("Error: Could not open file for reading: %s\n", strerror(errno));
return 1;
}
// Read lines from the file
while (fgets(buffer, sizeof(buffer), file) != NULL) {
printf("%s", buffer);
}
// Check if an error occurred
if (ferror(file)) {
printf("Error occurred while reading the file: %s\n", strerror(errno));
} else if (feof(file)) {
printf("\nEnd of file reached.\n");
}
// Close the file
fclose(file);
return 0;
}
Output (assuming data.txt
contains multiple lines of text):
First line of data
Second line of data
Third line of data
End of file reached.
Conclusion
The ferror()
function is useful for determining whether an error has occurred during input or output operations on a specified stream. It helps in managing I/O operations by allowing you to handle error conditions appropriately.
Comments
Post a Comment
Leave Comment