0
Deneme::Deneme(string FileName){

 fstream textfile;
 textfile.open(FileName);
 }

This gives me an error, but when I type textfile.open("randomname"); instead of textfile.open(FileName); there seems to be no problem. Why is this? It might be an easy question but I'm a beginner and couldn't find the solution of this.

baris_ercan
  • 152
  • 2
  • 16

3 Answers3

1

fstreams only accept const char*. Use textfile.open(FileName.c_str()); or fstream textfile(FileName.c_str()); instead (although C++11 accepts const std::string&). Here is a handy site to look up how the constructors and functions are declared.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166
0

fstream's open method takes a const char pointer while you're passing in a std::string by value, I imagine this could be the error. Try:

textfile.open(FileName.c_str());
Masci
  • 5,864
  • 1
  • 26
  • 21
0

Use a standard C++ conforming system: C++2011 provides constructors and open() functions taking a std::string const&. For a pre-C++2011 system you need to use name.c_str() to pass to the file stream.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380