1

I am using wide string file stream std::wofstream to open a file and read its contents. I used header fstream. But when i compile this code on XCode 7, it is showing following error

No matching member function for call to 'open'

my code was like

header used <fstream>

std::wofstream out;
out.open(filename, std::ios::binary); <--- error
* filename is wide char string

NOTE: It is working on windows fine with Visual Studio 2015.

Jai
  • 1,292
  • 4
  • 21
  • 41

1 Answers1

2

std::wofstream is just a std::basic_ofstream with a template type of wchar_t. std::basic_ofstream::open has two overloads per the standard

void open( const char *filename,
       ios_base::openmode mode = ios_base::out );
void open( const std::string &filename,                                  
       ios_base::openmode mode = ios_base::out );

As you can see neither of then take a wchar_t* or std::wstring. I suspect MSVS added an overload to accommodate using open with wide strings where xcode did not.

You should have no issue passing a std::string or a const char* to open()

I would like to point out that there is no reason to construct the object and then call open(). If you want to construct and open a file then just use the constructor which will do that.

std::wofstream out;
out.open(filename, std::ios::binary);

Becomes

std::wofstream out(filename, std::ios::binary);
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • so, should i convert filename to std::string ? – Jai Mar 03 '16 at 13:38
  • @Jai If you have it as a `std::wstring` then yes. You can see how to do that here: http://stackoverflow.com/questions/4804298/how-to-convert-wstring-into-string – NathanOliver Mar 03 '16 at 13:40