This code keeps giving me errors i dont know what Im doing wrong? How can I request the user to input the filename and then open that file and perform tasks with the data.
Example:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
ifstream inData;
ofstream outData;
string fileName;
cout << "Enter the data file Name : " << endl;//asks user to input filename
cin >> fileName; //inputs user input into fileName
inData.open(fileName); //heres where i try to open the file with the users input?
outData.open(fileName);
cout << fileName;
return 0;
}
The code keeps giving me errors. I have tried to use getline? I want the fileName to be string and not char.
You're passing std::string
objects to the parameters, not actual null-terminated char pointers. To do that, use c_str
:
inData.open(fileName.c_str());
outData.open(fileName.c_str());