-
file doesn't open
when i try to open a file for example c:\file.txt it will not open. here is my code.
Code:
int main ()
{
char c; /* User defined character */
char filename[50];
int num = 0; /* Number of times character was found */
FILE *inp; /* pointer to input file */
printf("Enter the name of the file you wish to open: ");
scanf ("%s", &filename);
inp = fopen("c:%s", "r");
if (inp != NULL){
printf("OPEN");
}else{
printf("NOT OPEN");
}
getch();
return 0;
}
it always comes back "NOT OPEN". why is this?
-
That's because you're literally tying to open the file "c:%s".
You need to put the path of the file you're actually trying to open there - a format specifier won't work.
-
well how would i be able to put a user specified file in there? would it be c:filename?
-
change
inp = fopen("c:%s", "r");
into
inp = fopen(filename, "r");
-
-
I may be wrong, but shouldn't
scanf ("%s", &filename);
be
scanf ("%s", filename);
since filename already is a pointer (an array, but they're practically the same thing).
-
yeah i guess it works either way