-
User inputted filename
Hi, I have a string which stores the name of the file the user has inputted, but I have no clue how to input this into the opening of that file!
I thought that fp=fopen("%s", filename, "r"); would work, but it obviosuly is incorrect as I get an error.
How can I get the string stored in filename into the filename area of the opening code?
-
You should look at the prototype of fopen first
Code:
FILE * fopen ( const char * filename, const char * mode );
the first paramters is then const char *, which mean it can take any char pointer. In your if your read the file name in to variable like
Code:
scanf("%s", filename);
The file will be your char * pointer. Send that as your first argument to fopon literally. Like
Code:
fopen(filename,"r");
PS: And ofcourse do all the error checking.
ssharish