Jump to content
  • Advertisement
Sign in to follow this  

filenames determined in program

This topic is 5045 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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

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


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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!