A lot of that sounds really wrong.
First of all, using backslashes in strings - you need to escape each backslash character ('\\'). [edit] And the console will escape input for you, so you don't need it for user input, that's important. And I forgot. [/edit]
The extension is part of the file name, don't remove it.
You cannot take a variable like filename and glue it to a file path without code to concatenate the string. Concatenate is a real word, by the way.
If you have a string that is big enough you can do something like:
Code:
#include <stdio.h>
#include <string.h>
void RemoveNewlines(char *s)
{
char *nl = strrchr(s, '\n');
if (nl!=NULL)
*nl = '\0';
}
int main(void)
{
char OpenMe[FILENAME_MAX] = "";
char enteredpath[FILENAME_MAX / 2];
char enteredname[FILENAME_MAX / 2];
FILE *fil = NULL;
printf("Please enter the file path:\n");
fgets(enteredpath, FILENAME_MAX / 2, stdin);
RemoveNewlines(enteredpath);
printf("Please enter the file name:\n");
fgets(enteredname, FILENAME_MAX / 2, stdin);
RemoveNewlines(enteredname);
strcat(OpenMe, enteredpath);
strcat(OpenMe, enteredname);
printf("Opening %s...\n", OpenMe);
fil = fopen(OpenMe, "r");
if (fil!=NULL)
{
printf("good\n");
fclose(fil);
}
return 0;
}
/*
Please enter the file path:
C:\Users\Josh2\My Documents\
Please enter the file name:
hello.txt
Opening C:\Users\Josh2\My Documents\hello.txt...
good
*/
Of course, reading the path requires reading in spaces so scanf(%s", enteredpath); is not sufficient. Use fgets and remove any '\n' in the path.