I used this to convert from string to char constchar * cpath = path.c_str();
now I found I need to trim the string but const denies to change it. So is there a variant which allows to change the variable?
If you need to trim the string, why not do it while it was still a string. In fact, why do you need to turn it into a C-string anyway? Why don't you just pass the actual string object around?
Otherwise, getting a char* from a std::string would be returning a pointer to the internal data. That's too dangerous for std::string because it would let someone modify the internal data without considering any other internal methods such as updating the size, re-allocating memory, etc.
NT3:
To answer your question, I am testing the string path:
if( stat(cpath,&s) == 0 )
stat needs char. Maybe I did not choosed correct way how to do it. My idea was to trim dot from left for the case directory name is like ./myDir , but then I realized that there can be file name like .myfile so not good idea. I should test first two characters and if they are ./ so remove 2 and add current_path to begin of the variable.
JLBorges: But in your 2nd example if I want to join this:
1 2 3 4 5
constchar* cpath = path.c_str() ;
if( path.find( ".\\" ) == 0 ){ // begins with .\
cpath += 2 ; // skip the first two chars
cpath = *workingPath + cpath; // join the value of workigPath with cPath
}
cpath = *workingPath + cpath Here you are adding integral value of first character of working path to cpath pointer.
You need either:
1) Work with strings and take c-string as last step:
MiiNiPaa
thanks, your solution works. But I am too tired to think about is father more. Just question to the second solution. Is it possible to use similar solution but for char * buffer? But the first solution is clear and simple. Simple to remember.