Jump to content
  • Advertisement
Sign in to follow this  
cyberflame

filenames determined in program

This topic is 4949 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would make a file, using fstream, out of a variable in the program. i want to create a file with the users name as the title EX: username.txt

Share this post


Link to post
Share on other sites
Advertisement
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

main()
{
cout << "Enter the your name." << endl;
string UserName;
getline(cin, UserName);
fstream UserNameFile(UserName.c_str(), ios::out | ios::trunc);

//Put stuff in the file

UserNameFile.close();
}

Share this post


Link to post
Share on other sites
You can find out that sort of information simply by doing a quick search on Google.

For example, you should try "c_str() c++" as your keyword.

This is what I found after about 30 seconds of searching:

const char* c_str() const;

For compatibility with "older" code, including some C++ library routines, it is sometimes necessary to convert a string object into a character array ("C-style string"). This function does the conversion. For example, you might open a file stream with a user-specified file name:

string filename;
cout << "Enter file name: ";
cin >> filename;
ofstream outfile (filename.c_str());
outfile << "Data" << endl;

Tells you pretty much all you need to know right? In just 30 secs of research...!

Remember: Google is your friend!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!