FERROR()

this function checks if an error occurred during a previous I/O operation on the given FILE* stream. it will return an error code that is associated with the FILE rather than the system (perror).

FILE *fp = fopen("data.txt", "r");
if (fp == NULL) {
    perror("File open error");
    return 1;
}

// Try reading
int c = fgetc(fp);
if (ferror(fp)) {
    perror("Read error");
}

fclose(fp);

Last updated