12

I get error when I try to do this:

path p = "somepath";
FILE* file = fopen(p.c_str(), "r");

I get:

argument of type "const boost::filesystem::path::value_type *" is incompatible with parameter of type "const char *"

Could anyone tell me what I'm doing wrong? Thanks

Martin
  • 1,877
  • 5
  • 21
  • 37

1 Answers1

14

If you're under Windows, that value_type is wchar_t, and will fail in the conversion for fopen (that needs a char*). As per the documentation, it seems you have to use the string() method to obtain a standard string with a default code conversor (wchar_t -> char):

FILE* file = fopen(p.string().c_str(), "r");
Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87
  • 5
    It should be noted that under certain circumstances the C++ standard library (e.g., `fopen()`) cannot be used to open a file on Windows, because the encoding Windows expects for the filename passed to `fopen` is incapable of representing that file's name. It's unclear when or if Windows will ever fix this bug. In the meantime Windows offers a non-standard function, _wfopen, as a workaround. So `_wfopen(p.c_str(),L"r")` should work. – bames53 Jul 05 '12 at 21:34