title: C Program unable to create output text file
body: <p>A friend of mine needs to use MATLAB for one of his classes, so he called me up (a Computer Science Major) and asked if I could teach him C. I am familiar with C++, so I am also familiar with the general syntax, but had to read up on the IO library for C.</p>
  
  <p>I was creating some simple IO programs to show my friend, but my third program is causing me trouble. When I run the program on my machine using Eclipse (with the CDT) Eclipse's console produces a glitchy output where instead of prompting me for the data, it gets the input and then prints it all at once with FAILURE.</p>
  
  <p>The program is supposed to get a filename from user, create the file, and write to it until the user enters a blank line.</p>
  
  <p>When I compile/run it on my machine via console (g++ files2.c) I am prompted for the data properly, but FAILURE shows up, and there is no output file.</p>
  
  <p>I think the error lies with how I am using the char arrays, since using scanf to get the filename will create a functional file (probably since it ignores whitespace), but not enter the while loop.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #define name_length 20
  #define line_size 80
  
  int main() {
    FILE * write_file;                    // pointer to file you will write to
    char filename[name_length];           // variable to hold the name of file
    char string_buffer[line_size];        // buffer to hold your text
  
    printf("Filename: ");                 // prompt for filename
  
    fgets(filename, name_length, stdin);  // get filename from user
  
    if (filename[name_length-1] == '\n')  // if last char in stream is newline,
      {filename[name_length-1] = '\0';}   // remove it
  
    write_file = fopen(filename, "w");    // create/overwrite file user named
  
    if (!write_file) {printf("FAILURE");} // failed to create FILE *
  
    // inform user how to exit
    printf("To exit, enter a blank line (no spaces)\n");
  
    // while getting input, print to file
    while (fgets(string_buffer, line_size, stdin) != NULL) {
      fputs(string_buffer, write_file);
      if (string_buffer[0] == '\n') {break;}
    }
  
    fclose(write_file);
    return 0;
  }
  </code></pre>
  
  <p>How should I go about fixing the program? I have found next to nothing on user-terminated input being written to file.</p>
  
  <p>Now if you will excuse me, I have a couple of files to delete off of my University's UNIX server, and I cannot specify them by name since they were created with convoluted filenames...</p>
  
  <p>EDIT------</p>
  
  <p>Like I said, I was able to use </p>
  
  <pre><code>scanf("%s", filename);
  </code></pre>
  
  <p>to get a working filename (without the newline char). But regardless of if I use scanf or fgets for my while loop, if I use them in conjunction with scanf for the filename, I am not able to write anything to file, as it does not enter the while loop.</p>
  
  <p>How should I restructure my writing to file and my while loop?</p>