I have this, this code has a hardcoded file name that will write information to it. How can I modify this so the user specifies the file name? I'm reading my book it's something like: create a variable string, prompt user, read it in, filename.c_str( all of these peices but I can't seem to figure it out.
#include <iostream>
#include <fstream>
#include <cstdlib>
usingnamespace std;
int main()
{
ofstream outClientFile ("a file name .txt");
//error checking
if (!outClientFile)
{
cerr << "File could not be opened." << endl;
exit(1);
}
int account;
char name [30];
double balance;
//read in from cin and then place it into a file
while (cin >> account >> name >> balance )
{
outClientFile << account << ' ' << name << ' ' << balance << endl;
//cout << "? ";
}
return 0;
}
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
usingnamespace std;
int main()
{
ofstream outClientFile;
string fileName;
cout << "Enter the file name : ";
cin >> fileName;
outClientFile.open(fileName.c_str());
//error checking
if (!outClientFile)
{
cerr << "File could not be opened." << endl;
exit(1);
}
int account;
char name [30];
double balance;
//read in from cin and then place it into a file
while (cin >> account >> name >> balance )
{
outClientFile << account << ' ' << name << ' ' << balance << endl;
//cout << "? ";
}
return 0;
}