filenames determined in program
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
#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();
}
#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();
}
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!
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!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement