-
RWCString to char
Hello,
Can someone please tell me whether there is a way to cast RWCString to char* ?
I have :-
RWCString name = "...";
char nnnnnnn[100] = "";
and I want to copy the contents of name to nnnnnnn.
I know I can do :-
char m_nnnnnnn[1000] = "";
strcpy(m_nnnnnnn, name);
but, I was hoping to do something nicer than that.
Any ideas please ?
Many thanks.
-
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?
-
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().
Danny Kalev
-
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.
Last edited by drkybelk; 04-16-2010 at 05:19 PM.
DKyb-------------------------------
Life is a short warm moment -
Death is the long cold rest.
Pink Floyd
-------------------------------
Similar Threads
-
By jamestmfbong in forum C++
Replies: 2
Last Post: 05-04-2009, 06:03 PM
-
By [gx]Shadow in forum Java
Replies: 5
Last Post: 10-25-2006, 10:20 PM
-
Replies: 9
Last Post: 07-28-2005, 08:40 PM
-
By Gastao Woelfert in forum VB Classic
Replies: 2
Last Post: 09-01-2000, 11:36 AM
-
By Gastao Woelfert in forum VB Classic
Replies: 0
Last Post: 09-01-2000, 08:59 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks