Techflash
Techflash

Reputation: 43

C++ - Convert from std::filesystem::path to const char

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.

Upvotes: 3

Views: 6382

Answers (3)

user14241435
user14241435

Reputation:

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

Upvotes: -3

Remy Lebeau
Remy Lebeau

Reputation: 595422

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

Upvotes: 6

VorpalSword
VorpalSword

Reputation: 1293

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

Upvotes: -1

Related Questions