0

I am writing a basic code but run into an error when trying to open a file. I've had a rough break and am having to start from the basics. Following is the part of the code where I run into the error:

int main()
{
    string name; 
    fstream file; 
    cout << " Enter file name and type (E.g filname.txt) : "; 
    cin >> name; 
    file.open(name);

Following is the error:

[Error] no matching function for call to 'std::basic_fstream<char>::open(std::string&)'

Screenshot of the error

I am returning after a long break so I apologize for any inconsistencies.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Temp Estcrowd
  • 73
  • 1
  • 5
  • This means you have an old compiler or at least one that is running in a mode prior to the c++11 / 2011 standard. – drescherjm May 30 '20 at 16:15

2 Answers2

3

If the std::basic_fstream::open(std::string&) overload isn't available you are probably compiling using some C++ version prior to C++11.

Make sure you compile using at least C++11 and it should be fine.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
-3

you have to pass the open mode too.

Here is an example:

// print the content of a text file.
#include <iostream>     // std::cout
#include <fstream>      // std::ifstream

int main () {
  std::ifstream ifs;

  ifs.open ("test.txt", std::ifstream::in);

  char c = ifs.get();

  while (ifs.good()) {
    std::cout << c;
    c = ifs.get();
  }

  ifs.close();

  return 0;
}

Code taken from Here

I suggest you to always check on cplusplus.com

It's very well documented!