I need to use it to read the file name from the user. I tried to use fgets but my program is terminating abnormally. How to do error handling for fgets ? Is it sufficient to check if the returned char pointer is null ?? Right now I'm using gets, but I've been told that its dangerous and my gcc compiler gives warning.
Code:#include <stdio.h> int main(void) { char fname[50]; FILE *fp; printf("Enter file name\n"); gets(fname); fp = fopen(fname, "r"); if(fp == NULL) { fprintf(stderr, "%s[%d] : ERROR WHILE OPENING THE FILE\n",__FILE__, __LINE__); return (-1); } return 0; }