Home > Net >  (SOLVED) C - Convert from std::filesystem::path to const char
(SOLVED) C - Convert from std::filesystem::path to const char

Time:10-04

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.

CodePudding user response:

From the c reference, you can use

outfilename.c_str()

to extract the path name as a character string.

CodePudding user response:

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).

CodePudding user response:

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();

CodePudding user response:

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

  • Related