0

What i wanna do is that take the input from the user and pass it to a function and use that input as the file name.The myfile is a global variable of type fstream that i have used in my program.The error statement is:

[Error] no matching function for call to
std::basic_fstream<char>::open(std::string&, const openmode&)
void displayRecord(string filename) {
    string s;
    myfile.open(filename, ios::in); // error here
    while (!myfile.eof()) {
        myfile >> s;
        cout << s << endl;
    }
    myfile.close();
}
user4581301
  • 29,019
  • 5
  • 26
  • 45
mkz
  • 21
  • 3
  • What is `myfile`? Also, you've surely forgotten to include `` – kesarling Jul 04 '20 at 16:24
  • 3
    [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – molbdnilo Jul 04 '20 at 16:25
  • d4rk4ng31, this is a 200+ lines code, i have included the libraries necessary, i just need help for this string thing – mkz Jul 04 '20 at 16:31
  • Make sure you are compiling to the C++ 11 Standard revision or a more recent revision. Prior to C++ 11 there was no `open` that accepted a `std::string`. You had to use a null-terminated `char` array. – user4581301 Jul 04 '20 at 16:32
  • 2
    Possibly using a pre c++11 standard library? If so fstream only takes `const char*` not `std::string`, try `myfile.open(filename.c_str(),ios::in);`. Otherwise please provide a [mre] – Alan Birtles Jul 04 '20 at 16:34
  • RE: `d4rk4ng31, this is a 200+ lines code` That does not matter as you simply have to use `search` function in your editor to search for `#include ` ;) – kesarling Jul 04 '20 at 16:35
  • Normally one of the best ways to attack a compiler error is to not have 200 lines of code. Write a few lines of code. Compile. Test. Add more code only when there are no compiler errors and the program behaves exactly as expected when run. If you have a bug, odds are really good that it is either in or related to the code just added. This usually makes it very easy to isolate the problem into a small program you can experiment with. If you already have 200 lines of code, Back up your program and start removing stuff until the error goes away. The error probably in the last removed code. – user4581301 Jul 04 '20 at 16:37
  • @d4rk4ng31 your proposed edit broke the error message. Quotes can't handle <>. They think it's an unknown XML tag and discard it. – user4581301 Jul 04 '20 at 16:44
  • @user4581301, sorry :( – kesarling Jul 04 '20 at 16:47
  • @d4rk4ng31 Nothing to be sorry about. That's just information so you know why I edited your edit. – user4581301 Jul 04 '20 at 16:51
  • Thanks @AlanBirtles it helped – mkz Jul 04 '20 at 16:57

0 Answers0