Opening a file issue

Hello..

i'm a beginner in this world (c++) and i need to open a file (i know that the function to be used is fopen) but i need the program asks for the text to be loaded. I got this.

#include<stdio.h>

int main(){
FILE* fptr;
char filename[10];
printf ("Please type the name of the file to be loaded:");
scanf("%c",filename);
if((fptr=fopen("filename","r"))==NULL){
printf("Cannot open the file\n");
}else{
printf("File opened successfully for reading\n");
}
fclose(fptr);
return 0;
}

But unfortunately it doesn't work. what can i do??? I would thank to someone help me with this little problem. Also i got a segmentation fault problem..What is that????

Thanks in advance..
Last edited on
You are opening file for reading but file doesn't exist because "r" reads only existing file.
thanks for answering.... i forgot to say that the file does exist.
Try char *filename instead of char filename[10] and change "filename" to filename
Hello.. thanks packman for trying to help me

i did whay you told me, but now i have a segmentation fault problem... do you know what is that
Last edited on
Topic archived. No new replies allowed.