Opening a user-inputed file

Started by
2 comments, last by Nazrix 23 years, 10 months ago
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
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Advertisement
#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
Thanks. That''s simpler than I thought
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
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);
}
}
http://www.crosswinds.net/~druidgames/resist.jpg

This topic is closed to new replies.

Advertisement