C examples for File:File Function
Demonstration error handling with perror() and errno.
#include <stdio.h> #include <stdlib.h> #include <errno.h> int main( void ) { FILE *fp;//from w ww. j av a 2 s .c o m char filename[80]; printf("Enter filename: "); gets_s(filename); if (( fp = fopen(filename, "r")) == NULL) { perror("You goofed!"); printf("errno = %d.\n", errno); exit(1); } else { puts("File opened for reading."); fclose(fp); } return 0; }