0

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++
};
alfC
  • 14,261
  • 4
  • 67
  • 118
Knotwood V
  • 51
  • 1
  • 6

1 Answers1

4

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.

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
alfC
  • 14,261
  • 4
  • 67
  • 118
  • thats isn't working for me const char* c = input.generic_string().c_str(); cout << c << endl; – Knotwood V Aug 24 '18 at 20:36
  • @KnotwoodV, "isn't working" is not a good description of your problem. (Also, you should improve the question if you you don't want it to be closed). I don't know how input was generated, but all works as expected (see the new code in my answer). – alfC Aug 24 '18 at 20:47
  • it just provides no output so i am assuming it hasn't passed any information across, @alfC if i knew what i should be describing , i wouldn't the question in the first place !!!! – Knotwood V Aug 24 '18 at 20:51
  • you code works but i dont understand why it doesn't when applied to my boost path – Knotwood V Aug 24 '18 at 20:59
  • No output is a valid output if the path is empty (`bfs::path p("")`), so perhaps `input` is empty. Also, you are shifting `<<` to a `char`, that is not the way to populate an array. I think there are more basic flaws than the usage of BFS in your code. – alfC Aug 24 '18 at 21:03
  • @KnotwoodV, see the new extra code. It does what you want. – alfC Aug 24 '18 at 21:06
  • thanks so it seems that it need to be called again - into boost::filesystem::path p(input); other wise it doesn't pass any data - if call like to const char* n1 = input.generic_string().c_str(); thank you, – Knotwood V Aug 24 '18 at 21:15
  • @KnotwoodV, I don't think that is the solution and I don't think you understand what the problem is, but I am sure you will learn soon. – alfC Aug 24 '18 at 21:22
  • 2
    `const char* c1 = p.string().c_str();` -- won't the temporary `std::string` instance returned by `p.string()` go out of scope after this statement, and thus the pointer returned by `c_str` becoming invalid? – Dan Mašek Aug 24 '18 at 23:12
  • 1
    @DanMašek, yes, you are right in general but it works for some reason. Actually there is a better function that what I showed which directly return the pointer. I updated my answer. – alfC Aug 24 '18 at 23:21
  • c_str returns pointer to a native string, which is wchar_t on Windows –  Aug 28 '18 at 06:11
  • @Ivan, I think you are right. However, from what I am seeing now, this is the closest thing to a `char*` observer from the `path` that one can get without generating a temporary to copy. – alfC Aug 28 '18 at 07:29