Very new to C programming. I'm writing a program where I want to prompt the user to input the name of the file to be open for reading. On my code shown bellow I want to throw an error if it doesn't open or if the file doesn't exist, but when I run it, my code blows up and I have to close the program (DOS)
/*ask user for the name of the file*/
printf("enter file name: ");
gets(fname);
//Opens the file from where the text will be read.
fp=fopen(fname, "r");
//Checks if the file us unable to be opened, then it shows the error message
if (fp == NULL)
{
printf("\nError, Unable to open the file for reading\n");
}
// you can test this by creating a name.txt file. Please let me know if you need additional information.
No comments:
Post a Comment