-
files
hi im trying to read from a file that has been created by the user.
my problem
file = fopen("data/hello.txt", "r");
is that. I want the user to enter the name of a file he/she wants to read rather than specifying it in the code...
how would i do that?
thanks
-
You can use a string as the file name.
Code:
char a[] = "data/hello.txt";
file = fopen(a, "r");
-Prelude
-
Or in your case you could request a filename from the user, placing it in a variable and then using the variable to open the file.
Code:
FILE *fd;
char file[256];
printf("What file would you like to open?\n ");
gets( file );
fd=fopen(file, "r");
-
Quote:
Originally posted by _Cl0wn_
Or in your case you could request a filename from the user, placing it in a variable and then using the variable to open the file.
Bad Fido. No cookie. Never use gets. Always assume your user will do everything in their power to break your user interface. Always. As such, never ever use gets.
Quzah.