converting boost::filesystem::path into char?

2019-09-22 09:17发布

i have worked out how to pass the boost path the required format, but i am having some issues figuring out hot to pass the path.stem into a char array, and then run some check on the filename and take the right action

need to read the filename and check for the next available number in the and then action, i was intending to use a for loop to get the number into an char array and then compare to this separate counter

how can i feed in the path() character by character into a array - or is there a better way !

int count(boost::filesystem::path input) {

cout << "inputzz :  " << input << endl;


char data;
wstring winput;
for (int a = 0; a < 4;){

//boost::filesystem::absolute(input).string();

//cout << input.generic_string() << endl;



(input.generic_string()) >> data;


data << (boost::filesystem::path()input.generic_string());


//a++
};

1条回答
ら.Afraid
2楼-- · 2019-09-22 09:21

Given a bfs::path p, p.c_str() gives you access access to the null-terminated char* array.

const char* c = p.c_str();

https://www.boost.org/doc/libs/1_63_0/libs/filesystem/doc/reference.html#c_str

Full example:

#include<iostream>
#include<boost/filesystem/path.hpp>

int main(){
    boost::filesystem::path p("~/.bashrc");
    const char* c = p.c_str();
    std::cout << c << '\n';

    char c2[99];
    std::strcpy(c2, p.c_str());
    std::cout << c2 << '\n';
}

From @Ivan's comment, it is likely that char is not the underlying representation in all systems. For that reason one migth need to use the value type of path, as in const boost::filesystem::path::value_type* c = p.c_str(); and modify the rest of the code, and for example use the generic std::copy.

查看更多
登录 后发表回答