Thank you, quzah.
For anybody's reference, here is the revised (and working!) code:
Code:
#define _CRT_SECURE_NO_WARNINGS
#define MAX_FILE 100 //maximum length of file_location string, including termination character
#define MAX_TEXT 50 //maximum length of text string, including termination character
#include <stdio.h>
int main()
{
char file_location[MAX_FILE];
char text[MAX_TEXT];
int i; //loop counter
FILE *myfile;
printf("Enter file location: "); //note: fopen doesn't like creating new directories
fgets(file_location, MAX_FILE, stdin);
for (i=0; i<MAX_FILE; i++) //replace newline character with string termination character
{
if (file_location[i] == '\n'){
file_location[i] = '\0';
}
}
printf("File location: %s\n",file_location);
myfile=fopen(file_location,"w");
if (myfile != NULL) //checks if file is valid
{
printf("Enter your text: ");
fgets(text, MAX_TEXT, stdin);
fprintf(myfile, text);
}
else
{
printf("INVALID FILE PATH.\n");
return 0;
}
fclose(myfile);
return 0;
}