but, I was hoping to do something nicer than that.
Any ideas please ?
Many thanks.
04-15-2010, 10:05 AM
jonnin
an array of char requires moving the items into it using the limited ways that arrays support:
a loop
one by one copy: a[0] = b[0], a[1] = b[1], ... etc
a copy (strcpy, memcpy, etc)
or the like, all of which are crude.
If you want something better, you need an object, even if its just a wrapper around a char array to give it an assignment operator.
The cleanest way then is maybe
char* cp;
string str;
str = your_RWCString;
cp = & str[0];
... use cp as if it were a C-string if that is needed for some reason, such as a byte-array packet for network transmission?
Or, if you do not actually need the char array at all, just use the string?
04-15-2010, 10:14 AM
Danny
IF you really have to use a char array, there's no escape from calling a function that will copy the buffer from name. Look at the std::copy algorithm or plain old strncpy().
04-16-2010, 05:11 PM
drkybelk
Just use
Code:
RWCString rws = "Copy me";
// 1000 should be big enough, but of course you can also dynamically allocate and delete later
char buffer[1000] = {0};
strcpy(buffer,rws.data());
But you might not need to copy to a buffer at all. It depends on what you want to do with it.
if you only need a const char* (for example as the input of an API call) then you shouldn't even
bother copying, but just use the data() method of RWCString.