Hi, Could someone tell me how to open a file in C based on user input.
This is what I have tried:

printf("enter the filename");
         scanf("%s",filename);
         fp1=fopen("filename","r");

Use &filename in the scanf() statement..

scanf("%s",&filename);

This is how I open file on the basis of user input.

FILE *fp;
char fnamer[100]="";		//Storing File Path/Name of Image to Display
printf("\n\nPlease Enter the Full Path of the Image file you want to view: \n");
scanf("%s",&fnamer);
fp=fopen(fnamer,"r");
        if(fp==NULL)
	{
		printf("\n%s\" File NOT FOUND!",fnamer);
		getch();
		exit(1);
	}

Use &filename in the scanf() statement..

scanf("%s",&filename);

This is how I open file on the basis of user input.

FILE *fp;
char fnamer[100]="";		//Storing File Path/Name of Image to Display
printf("\n\nPlease Enter the Full Path of the Image file you want to view: \n");
scanf("%s",&fnamer);
fp=fopen(fnamer,"r");
        if(fp==NULL)
	{
		printf("\n%s\" File NOT FOUND!",fnamer);
		getch();
		exit(1);
	}

We need to give the destination path for fnamer right? ie. c:/folder_name\...
This is what I've used

fp1=fopen("/cygdrive/d/Beta.txt","r");

So how do I integrate the filename into this?

FILE *fp2;
//Getting File Name and Setting File Path for New File.
			char fnamen[12]; //FileName
			char fnamew[100]=""; //Full Name with default path
			char ext[5]=".bmp";  //Extension =>Provide a default one or ask for it during input. [Not necessary in the present case]
			char dpath[50]="C:\\TC\\BIN\\GIFS\\"; //Default path

			printf("\n\nEnter Name for the New Image File (w/o path/extension):");
			scanf("%s",&fnamen);

                        //Integrating the diff parts of filename/path
			strcat(fnamew,dpath);
			strcat(fnamew,fnamen);
			strcat(fnamew,ext);

fp2=fopen(fnamew,"r");
if(fp2==NULL)
			{
				printf("Error Opening File");
				getch();
				exit(1);
			}

using strcat() to concatenate the strings.

>scanf("%s",&fnamen);
fnamen is already a pointer in this context. Adding a redundant address-of operator changes the type, which can potentially break your program (though it's usually benign in this case).

Also consider that %s without a field width is no better than gets (ie. there's no protection from buffer overflow), and the result of scanf should be tested for failure:

if (scanf("%11s", fnamen) != 1) {
    fputs("Invalid input\n", stderr);
    exit(EXIT_FAILURE);
}

>char fnamew[100]="";
I'd also like to point out that initializing fnamew to an empty string is absolutely critical if you're going to immediately call strcat, because strcat expects to be given a valid string. The following is broken because fnamew isn't a valid string (no null terminator):

char fnamew[100];
char dpath[50]="C:\\TC\\BIN\\GIFS\\";

strcat(fnamew,dpath);

Because of this subtlety, I often recommend using strcpy to populate the initial portion of the string, then strcat to append. Then it doesn't matter what the destination's current contents happen to be:

strcpy(fnamew,dpath);
strcat(fnamew,fnamen);
strcat(fnamew,ext);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.