Here is one possible way with a few improvements.
Code:
#include <stdio.h>
#define DIM 512
int main()
{
FILE *myfile = NULL; // Initialize all local variables.
char buff[DIM]= "";
myfile=fopen("test.txt", "w");
if(myfile == NULL) // Always test if the file was opened
{
printf("File open failed! Exiting.");
return 1; // EXIT_FAILURE in stdlib.h is better
}
printf("Please enter a name: ");
fgets(buff, DIM, stdin);
fprintf(myfile, "%s\n", buff);
fclose (myfile);
return 0; // EXIT_SUCCESS here as well
}