title: C - How to prompt user to enter filename
body: <p>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)</p>
  
  <pre><code>/*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");
      }
  </code></pre>
  
  <p>// you can test this by creating a name.txt file. Please let me know if you need additional information. </p>