title: Open file with user input (string) - C
body: <p>I'm trying to figure out how user input can be used as a filename in C. Here's the simple program I wrote.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #define MAX 100
  
  int main()
  {
      FILE *fp;
      char name[MAX];
  
  
      printf("Enter filename: ");
      fgets(name, MAX, stdin);
  
      if((fp = fopen(name, "w")) == 0)
          printf("File cannot be opened!");
  
      return 0;
  }
  </code></pre>
  
  <p>It always prints "File cannot be opened".</p>