Knotwood V
Knotwood V

Reputation: 51

converting boost::filesystem::path into char?

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++
};

Upvotes: 0

Views: 5232

Answers (1)

alfC
alfC

Reputation: 16242

GCC:

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

const char* c = p.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';
}

MSVC:

char is not the underlying representation on all systems. On Windows for example, it is wchar_t. For that reason one might 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.

Alternatively, an example code for converting a wchar_t * to a char * can be found here.

Upvotes: 4

Related Questions