filenames determined in program

Started by
3 comments, last by cyberflame 19 years, 4 months ago
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
[href]http://neoeden.web1000.com[/href]
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();
}
It works, but what does it do?

whats the .c_str() do?
[href]http://neoeden.web1000.com[/href]
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!
Trace back the elements...
thankyou bunches (i was searching for the answer, but not on google, i was searching on cplusplus.com [headshake])
[href]http://neoeden.web1000.com[/href]

This topic is closed to new replies.

Advertisement