In C++
, we can use the ferror()
function to check for errors in the input file stream. It returns a non-zero value if the error indicator is set.
int ferror(FILE* stream);
stream
It represents a pointer to the file stream for which we need to check errors. It is of FILE*
type.
0
: It returns if the file stream does not have any errors.
Non-zero
: It returns a non-zero value if errors are in the file stream.
#include<cstdio>
//Including headers#include <cstdio>#include <iostream>using namespace std;int main() {int ch;FILE* fp = fopen("file.txt","w");if(fp) {ch = getc(fp);if (ferror(fp))cout << "Can't read from file";}fclose (fp);return 0;}
Can't read from file