As "some programmer dude" stated, fopen takes a char * (a pointer to char), not a single char. Using an array will do, as he also stated.
I will just compile his comments into an answer.
#include <stdio.h>
int main()
{
printf("Path/Name of the file: \n");
char filename[128];
scanf("%127s", filename);
FILE *filePointer = fopen(filename, "r");
return 0;
}
I suggest you use this scanf syntax (%NUMBERs) to avoid an easy buffer overflow that could lead to a crash and a security flaw. Don't forget the additional \0 for your buffer size (I refer to the filename variable as your buffer), and be careful with user input in any real word application, especially when it is for something as important as accessing files. With an access to this program, one could just open any file even above this one in the file tree using ../ .
I also suggest you learn to read compiler output as it is very useful for problems like this one (don't worry you should naturally learn it over time after seeing similar errors often :) ).
In your case, the compiler explicitly tells you that the function wants a char * and you are giving it a char (it says const char * restrict but const and restrict are here to tell you that the pointer will not be modified in the function).
expected ‘const char * restrict’ but argument is of type ‘char’
I hope that you understand better what was wrong and that this was clear enough !
fopen
expects its first argument to be of typechar *
. You are passing achar
. – William Pursellgetchar
reads a single character, you can't simply treat this single character as a string. You probably want something likechar filename[128]; scanf("%127", filename);
– Some programmer dude