Opening a user-inputed file
I''ve been able to open a file that is hard-coded, but how do you ask the user what file to open then open it in VC++.
Thanks for any info
#include < stdio.h >void main(){ char fn[80]; FILE *fin; printf("enter file name:"); scanf("%s",fn); fin=fopen(fn,"r"); // do whatever u wanna do with "fin" here fclose(fin);}
- pouya
--------------
The trick to flight is to throw yourself at the ground and miss!!!
Edited by - pouya on May 31, 2000 9:16:40 PM
This is a safer and more stable way to do it. It checks the return value of "fopen()" for a NULL pointer. NEVER use a NULL pointer!
#include
void main(void)
{
FILE *fin;
char fn[80];
puts("Enter file name:");
scanf("%s", &fn);
if((fin=fopen(fn, "r+b"))!=NULL) { //check fin for NULL
rewind(fin);
//Add your code here
fclose(fin);
}
}
#include
void main(void)
{
FILE *fin;
char fn[80];
puts("Enter file name:");
scanf("%s", &fn);
if((fin=fopen(fn, "r+b"))!=NULL) { //check fin for NULL
rewind(fin);
//Add your code here
fclose(fin);
}
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement