954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

dynamic opened file by fopen

Hi all,

I just get stuck with this kind of simple problem. Such a novice i am.
Could you help me guys.:$

I try to built the code and for the first procedure i should open first the file. The file which i should opened its depend on the input i gave. Compiling it's OK. But It's not working. There no sign at all.

Here the code. (i am also attach kind of file i should open in this code)

#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <string.h>

main() 
{ 

    FILE *finput, *fopen();
    int i;
    char pfile[80];
    
	printf ("Type number of input (between 1 to 3): ");
    scanf ("%i", i);
    
    if (i = 1)
    pfile == "pf1";
    else if (i = 2)
    pfile == "pf2";
    else if (i = 3)
    pfile == "pf3";
   	else
   	 {printf ("Invalid Input, the number should between 1 to 3!!!");}
    
    
    finput = fopen(pfile, "r");
    	   
    if(finput==NULL) {printf("Error: can't open the file.txt\n");return 2;}
    else {printf("File opened successfully.\n");}      
	
    
    fclose(finput);
    system("pause");
	return(0);

	
}


Thank you in advanced

inC

Attachments pf.zip (0.36KB)
headacheinC
Newbie Poster
9 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Is this your first post? It isn't. Post source code in BB tag - click on # icon above the text area of message.

Had you compiled this code?

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

Well do you know what exactly you are doing or what you want to do ?

FILE *fopen(); // Why this ?



And what are you expecting the computer to display when you are just opening the file and closing it ?

finput = fopen(pfile, "r");

if(finput==NULL) {printf("Error: can't open the file.txt\n");return 2;}
else {printf("File opened successfully.\n");}

fclose(finput);


And why so much complexity?char pfile[80];
printf ("Type number of input (between 1 to 3): ");
scanf ("%i", i); // Error here in scaning (no &)
if (i = 1)
pfile == "pf1";
else if (i = 2)
pfile == "pf2";
else if (i = 3)
pfile == "pf3";
else
{printf ("Invalid Input, the number should between 1 to 3!!!");}
finput = fopen(pfile, "r");
The complete thing above can be done just by:

char pfile[80];
printf ("Type number of input (between 1 to 3): ");
scanf ("%i",&i);
sprintf(pfile,"pf%d",i);
finput=fopen(pfile,"r");
csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

My mistake with *fopen()...
yes i compiled it..not error message appears..

phew..thanks..guyss

headacheinC
Newbie Poster
9 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You