1

I have this code that creates a std::filesystem::path from another existing path:

std::filesystem::path outfilename = PathToNewVersionTXT;

Is it possible to convert outfilename to a const char* type? As that is what is required later down in the code.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Techflash
  • 25
  • 1
  • 6
  • SOLVED BY USING STANDARD C `fopen` – Techflash Oct 03 '21 at 23:36
  • Does this answer your question? [Convert filesystem::path into char\* on windows](https://stackoverflow.com/questions/54109159/convert-filesystempath-into-char-on-windows) – malat Apr 20 '23 at 09:52

3 Answers3

3

Use the path::string() or path::u8string() method to get the path as a std::string, and then you can use the string::c_str() method:

std::filesystem::path outfilename = PathToNewVersionTXT;
std::string outfilename_str = outfilename.string(); // or outfilename.u8string()
const char *outfilename_ptr = outfilename_str.c_str();
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Now I get only 2 errors, both on line 63, line 63 being: ```curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);``` The errors being: `non-standard use of class 'std::basic_ofstream>' as an argument to a variadic function` as well as 'std::basic_ofstream>::basic_ofstream(const std::basic_ofstream> &)': attempting to reference a deleted function` – Techflash Oct 03 '21 at 21:05
  • @Techflash that issue has absolutely nothing to do with your question of converting a filename path to a character string. You need to post that issue as a new question. One question per post, please. But, I can tell you right now, you can't pass a `std::ofstream` *by value* to a function parameter. Which is what the error messages are complaining about you doing. – Remy Lebeau Oct 03 '21 at 21:10
  • Platform independency.... Linux: "obj.path().c_str()" works. Windows -> Error. Solution "obj.path().string().c_str()" – Anonymous Dec 26 '21 at 20:28
-1

It looks like you just need to use std::filesystem::path::c_str to get a pointer to the w_char_t (windows) or char (Linux).

You can’t change it to const char Because that’s just a single character and you want multiple characters to make up a pathname. You really want a const char* which is what this function returns (Linux).

VorpalSword
  • 1,223
  • 2
  • 10
  • 25
  • Got kinda close: I figured out this: `const char* outfilenamestr = std::filesystem::path::c_str(outfilename);` But it still gives an error: E0245 a nonstatic member reference must be relative to a specific object – Techflash Oct 03 '21 at 20:14
  • @Techflash because that syntax is wrong, you would need `const char* outfilenamestr = outfilename.c_str();` instead. Which will work only on Posix systems, but not on Windows. – Remy Lebeau Oct 03 '21 at 21:08
-2

Use the c_str() function for this. E.g. const char* str = path.c_str();.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • Didn't work: E0144 a value of type "const std::filesystem::path::value_type *" cannot be used to initialize an entity of type "const char *" – Techflash Oct 03 '21 at 20:08
  • That's probably because value_type* has wchar_t* not char*. –  Oct 03 '21 at 20:13
  • Try converting it using a std::stringstream. For example std::stringstream sstream; sstream << outfilename; const char* x = sstream.str().c_str() –  Oct 03 '21 at 20:14
  • That new code just gives me: no instance of constructor "std::basic_ofstream<_Elem, _Traits>::basic_ofstream [with _Elem=char, _Traits=std::char_traits]" matches the argument list – Techflash Oct 03 '21 at 20:17
  • Make sure you #include –  Oct 03 '21 at 20:21
  • this works for me: `std::filesystem::path path("hello"); std::stringstream sstream; sstream << path; std::string convertedstr = sstream.str(); const char* ptr = convertedstr.c_str(); // then use a function which accepts the const char* std::printf(ptr);` –  Oct 03 '21 at 20:22
  • Just added , no difference. – Techflash Oct 03 '21 at 20:23
  • Like this? outfilenameNonstr is the filesystem::path, outfilename is what I want to be the char thing `std::filesystem::path outfilenameNonstr; std::stringstream sstream; sstream << outfilenameNonstr; std::string outfilename = sstream.str(); const char* ptr = outfilename.c_str();` – Techflash Oct 03 '21 at 20:26
  • are you sure you're using std::stringstream? you may be using std::ofstream? –  Oct 03 '21 at 20:27
  • I am using ofstream, is there an issue with that? Later in the code: `ofstream fp(outfilename);` – Techflash Oct 03 '21 at 20:27
  • yes i believe that should work. just make sure the std::string outfilename doesn't go out of scope otherwise `ptr` will be pointing to something which no longer exists –  Oct 03 '21 at 20:28
  • It sounds like you need to include too –  Oct 03 '21 at 20:30
  • If that should work, why am I getting all of these? `'std::filesystem::path outfilenameNonstr': redefinition`, `non-standard use of class 'std::basic_ofstream>' as an argument to a variadic function`, `'std::basic_ofstream>::basic_ofstream(const std::basic_ofstream> &)': attempting to reference a deleted function` – Techflash Oct 03 '21 at 20:31
  • These are all of my `#include`s ```#include #include #include #include #include #include #include #include #include #include #include ``` – Techflash Oct 03 '21 at 20:32
  • those errors are unrelated. you seem to be copying an object of std::ofstream which you can't do –  Oct 03 '21 at 20:36
  • Maybe the line numbers would give more context: Error 1: `std::filesystem::path outfilenameNonstr; std::stringstream sstream; sstream << outfilenameNonstr; std::string outfilename = sstream.str(); const char* ptr = outfilename.c_str();` ||||| Error 2 & 3: `curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);` – Techflash Oct 03 '21 at 20:37