0

Possible Duplicate:
C++ type of argument to ifstream::open()
C++ ifstream error using string as opening file path.

Whats wrong with

ifstream sourceFile;
sourceFile.open(filepath);

I am getting something about:

no matching function call to ifstream::open(string&)

Whats wrong?

Same with

ifstream sourceFile(filepath)

Where filepath is a string

Community
  • 1
  • 1
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805

3 Answers3

2

As of C++11, iostreams can take a string as the parameter to name the file to be opened -- but this was added in C++ 11, so quite a few libraries don't include it yet. Updating to the latest version of your compiler/library might help (but then again, it might not -- I doubt that support is quite universal even yet).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

Looks like filepath is a string. Open only takes raw pointers. So do this - Open(filepath.c_str());

Superman
  • 3,027
  • 1
  • 15
  • 10
0

It takes a char* filename and not a string as argument.

sourceFile.open(filepath.c_str());

The Function is std::ifstream::open
    void open ( const char * filename, ios_base::openmode mode = ios_base::in );

(Reference)

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191