简体   繁体   中英

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM