when assigning a path name to a file stream is there any way to make the extension the part after the period a variable and tack it onto the name of the file?? If so how
Printable View
when assigning a path name to a file stream is there any way to make the extension the part after the period a variable and tack it onto the name of the file?? If so how
Not quite sure what you mean. If you want to extract the extension from a filename, look up fnsplit in dir.h. This is not an ANSI C function however.
I would take a look at strings, it would have answered your two posts:
Code:
string filename = "myfile";
string ext = ".txt";
filename+=ext;
fstream file;
file.open(filename, ios:: out);
When I was making a login in program I need to do the same thing you are asking. I will post my signup function below. It shows how to do this.
Be sure to include <string.h>.Code:void signup()
{
char name[10],pass[10],filename[12],combo[19];//variables I need
cout << "Enter 8 character user name:";
cin >> name;
cout << "Enter 8 characater password:";
cin >> pass;
strcpy(filename,name);//copy name to filename so i have two to manipulate
strcat(filename,".txt");//add ".txt" to the end of file name
strcpy(combo,name);//copy name to combo
strcat(combo,".");//add a dot to the end of combo
strcat(combo,pass);//add pass to the end of combo
ofstream a_file(filename);//create a file named "username.txt"
a_file<<combo;//write the text combo into it
cout<<"\n\nYou Have Successfully Signed Up.\n\n";
system("PAUSE");
}
If you need more explaination just say so and I will help you more with it. I think my commented code will be enuff though. and <fstream.h>