ferror() in C Last Updated : 03 Feb, 2025 Comments Improve Suggest changes Like Article Like Report In C, ferror() is a built-in function used to check errors in files during file operations. It provides a simple way to do file operations without any interruption in your C program. Syntaxferror() is a standard library function defined in <stdio.h> file.ferror(fptr)Parameter:Take as a file stream in parameter.Return Value:If the file has error, it returns a non-zero value.Otherwise, it returns 0.Note: To check error in file using ferror(), file must be open before it.Examples of ferror()The following examples demonstrate the use of ferror() in C programs.Check Error When Writing Data C #include <stdio.h> int main() { FILE *fptr = fopen("gfg.txt", "w"); // Write data to the file fprintf(fptr, "Hello, GFG!"); // Check error after writing data into file if(ferror(fptr)==0) printf("Data written successfully."); fclose(fptr); return 0; } OutputData written successfully.Check Error When Read Data C #include <stdio.h> int main() { FILE *fptr = fopen("gfg.txt", "w+"); fprintf(fptr, "GeeksForGeeks!"); rewind(fptr); char d[14]; while (fscanf(fptr, "%s", d) != EOF) // check error after reading data from file if(ferror(fptr)==0){ for(int i=0; i<sizeof(d); i++){ printf("%c", d[i]); } printf("\nNo error, Data read successfully"); } fclose(fptr); return 0; } OutputGeeksForGeeks! No error, Data read successfully Comment More infoAdvertise with us Next Article ferror() in C R ritikk5g6k Follow Improve Article Tags : C Language C-Functions File Handling Similar Reads clearerr() in C In C, file handling errors can be handled using ferror() and feof() functions. But these error flags persists until they are cleared. clearerr() is a built-in function used to reset errors when you are doing read-and-write operations with the file.Example:C#include <stdio.h> int main() { FILE 1 min read Error Handling in C In C programming, error handling is typically done using functions that handle runtime errors, returning error codes or messages to notify the programmer about the failure or incorrect operation. Since C does not provide built-in exception handling like other high-level languages (e.g., try-catch in 8 min read fwrite() in C In C, fwrite() is a built-in function used to write a block of data from the program into a file. It can write arrays, structs, or other data to files but is especially designed to write binary data in binary files.Example:C#include <stdio.h> int main() { FILE *fptr = fopen("gfg.bin", "wb"); i 3 min read fprintf() in C fprintf is used to print content in file instead of stdout console. int fprintf(FILE *fptr, const char *str, ...); Example: C // C Program for the above approach #include<stdio.h> int main() { int i, n=2; char str[50]; //open file sample.txt in write mode FILE *fptr = fopen("sample.txt 1 min read C - File I/O In this article, we will learn how to operate over files using a C program. A single C file can read, write, move, and create files in our computer easily using a few functions and elements included in the C File I/O system. We can easily manipulate data in a file regardless of whether the file is a 11 min read Like