A few pointers then:
>>scanf ( "%20s", filename );
The FAQ link I posted tries to get people to avoid scanf() for reading strings from the keyboard.
Your comment:
/* Constants */
is not appropriate, as there are no constants.
>>char file_extension[5] = ".txt\0";
The \0 is not needed, it's added by the compiler.
>>fp = fopen ( path , "a" );
Mode "a" doesn't create a new file, like your function name suggests. Either make it mode "w" or change the function name.
As for the size of the filename array, you can use the constant FILENAME_MAX like so:
>>char filename[FILENAME_MAX];
Another way to achieve a similar result (one of many):
Code:
#include <stdio.h>
#include <string.h>
void Create_New_Excavation_File(void)
{
FILE *fp;
const char path[] = "C:\\Directory";
const char file_extension[] = ".txt";
char fullname[FILENAME_MAX];
char input[FILENAME_MAX - sizeof(path) - sizeof(file_extension)];
puts("Please enter an appropriate name for the new file you wish to create.\n"
"(Maximum 20 characters)");
if (fgets(input, sizeof(input), stdin) != NULL)
{
char *p = strchr(input, '\n');
if (p) *p = '\0';
sprintf(fullname, "%s\\%s%s", path, input, file_extension);
printf("\nCreating File %s.....\n", fullname);
if ((fp = fopen(fullname, "w")) == NULL)
{
perror(fullname);
}
else
{
fprintf(fp, "\n line of text .");
fclose(fp);
}
}
}
int main(void)
{
Create_New_Excavation_File();
return(0);
}