-2

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.

jww
  • 83,594
  • 69
  • 338
  • 732
user1725435
  • 11
  • 1
  • 2
  • 6

1 Answers1

4

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());
0x499602D2
  • 87,005
  • 36
  • 149
  • 233