-
November 14th, 2008, 11:43 PM
#1
File output names
I'm trying to output a file with a set name.
like:
cin>>name;
newPlayer.open (name);
but I want it to set the file extension too, so it's like
cin>>name;
newPlayer.open (name".mbf");
but it constantly dislikes me lol
I don't know the logic behind this one. Help?!
-
November 15th, 2008, 01:22 AM
#2
Re: File output names
Well I figured it out, after a while of headache
I had to make a char, then convert it to a string, then back to a char. (yeah, it was a pain in the ***)
Code:
cout<<"Please enter your name: ";
cin>>name;
string name2=name;
const char* nameOut;
nameOut=name2.c_str();
name2+=".mbf";
cout<<"Wrote file: "<<nameOut;
newPlayer.open(nameOut);
newPlayer<<name;
newPlayer.close();
-
November 15th, 2008, 01:49 AM
#3
Re: File output names
You could have simply done the following:
Code:
std::string name;
std::cin >> name;
name = name + ".extension"; //whatever extension
std::fstream myfile(name.c_str());
That is, append the extension to the filename and then use the const char* returned by applying c_str() member to the fstream object to open the file.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|