Hi, I am writing a program to read from a file.
I want to ask the user for the file name to open.
I have tried to use the basic open file code from cplusplus.com and it won't work unless I specify the file name in the code. If I have them enter a file name, I then need to add "'s to it?
Either way, I get errors if I use char or string for the input file name. Can anyone show me how to do this?
Code from cplusplus.com with my variable for the fileName:
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
char fileName[256];
cout << "Enter a file";
cin >> fileName; //tried string instead of char with getline(cin, fileName)
//but same problem as below.
ofstream myfile;
myfile.open (fileName); //should have quotes around the file name
// ex. ("file.text")
// but how to put
//them in? Won't let me concatenate them in
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
Thanks for any help understanding this