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.

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.

The program is supposed to get a filename from user, create the file, and write to it until the user enters a blank line.

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.

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.

```
#include <stdio.h>

#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;
}

```

How should I go about fixing the program? I have found next to nothing on user-terminated input being written to file.

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...

EDIT------

Like I said, I was able to use 

```
scanf("%s", filename);

```

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.

How should I restructure my writing to file and my while loop?