hi,
i have two questions:
1. How to convert from string to char*? I use c_str() of string, but it
returns "const char*" instead of char*;
2. When i use copy() function of string, i always get some unknown
characters from the first parameter (char*) after using copy()
function.(these unknow character is like "<<" or ">>", but my string doesn't
include these characters.)

could anybody tell me how to resolve it , thanks.

Lorry

Re: How to convert from string to char*? by John

John
Wed Feb 28 00:05:39 CST 2007

"Lorry Astra" <LorryAstra@discussions.microsoft.com> wrote in message
news:654CE450-394B-4E24-967D-E8CDE85FBEF2@microsoft.com
> hi,
> i have two questions:
> 1. How to convert from string to char*? I use c_str() of string, but
> it returns "const char*" instead of char*;

One way is to create a char array and copy from c_str() to it.

> 2. When i use copy() function of string, i always get some unknown
> characters from the first parameter (char*) after using copy()
> function.(these unknow character is like "<<" or ">>", but my string
> doesn't include these characters.)

Using copy won't nul-terminate the string, so you will get junk characters
after the characters you copy. If you use this approach, then you need to
nul-terminate manually. If this doesn't solve the problem, then post
(compileable) code showing what you are doing.

--
John Carson



Re: How to convert from string to char*? by Tamas

Tamas
Wed Feb 28 15:18:49 CST 2007

Lorry Astra wrote:

> 1. How to convert from string to char*? I use c_str() of string, but it
> returns "const char*" instead of char*;

The question is what you want to do with that char*.

If you have a poorly written or old function that takes a read-only
char* input, but without the const keyword, just use the &str[0] expression:

void OldCCall(char* filename);
// some old 3rd party API call, improperly omitting the const keyword

void ModernCall(const std::string& filename)
{
std::string filename_tmp(filename);
OldCCall(&filename_tmp[0]);
}

This technique is mostly useful if you are certain that OldCCall treats
the string as read-only. If OldCCall tries to write beyond
filename.size(), you will get into trouble. I prefer this solution to
const_cast, because I have no way of knowing if OldCCall actually writes
its input (for whatever sloppy reason). If an API call omits the const
keyword, assume the worst.

You can also use string just as a memory buffer:

std::string buff(length, 0);
char* s = &buff[0];

And then you can write to this buffer, but you can't write more than
length characters/bytes to it. Just remember that 0-terminating this
buffer early won't make the string's size smaller -- buff.size() is
always length in this example. You would use strlen(buff.c_str()) to get
the size of the contained C-string.

Tom

Re: How to convert from string to char*? by John

John
Wed Feb 28 17:21:41 CST 2007

"Tamas Demjen" <tdemjen@yahoo.com> wrote in message
news:u%23QiL43WHHA.4872@TK2MSFTNGP03.phx.gbl
> Lorry Astra wrote:
>
>> 1. How to convert from string to char*? I use c_str() of string, but
>> it returns "const char*" instead of char*;
>
> The question is what you want to do with that char*.
>
> If you have a poorly written or old function that takes a read-only
> char* input, but without the const keyword, just use the &str[0]
> expression:

In practice, this may work, but it is not a supported use in terms of the
C++ standard (you can do that with a vector of chars, provided its capacity
is large enough, but not with a string).


--
John Carson



RE: How to convert from string to char*? by Ben

Ben
Thu Mar 01 09:05:08 CST 2007

string::data()


"Lorry Astra" wrote:

> hi,
> i have two questions:
> 1. How to convert from string to char*? I use c_str() of string, but it
> returns "const char*" instead of char*;
> 2. When i use copy() function of string, i always get some unknown
> characters from the first parameter (char*) after using copy()
> function.(these unknow character is like "<<" or ">>", but my string doesn't
> include these characters.)
>
> could anybody tell me how to resolve it , thanks.
>
> Lorry

Re: How to convert from string to char*? by beginthreadex

beginthreadex
Thu Mar 01 09:42:43 CST 2007

string str;
char* sz;

str = "magic";
sz = (char*)malloc(str.length() + 1);
strcpy(sz, (char*)str.c_str());
printf("%s", sz);
free(sz);


On Tue, 27 Feb 2007 21:11:08 -0800, Lorry Astra
<LorryAstra@discussions.microsoft.com> wrote:

>hi,
> i have two questions:
>1. How to convert from string to char*? I use c_str() of string, but it
>returns "const char*" instead of char*;
>2. When i use copy() function of string, i always get some unknown
>characters from the first parameter (char*) after using copy()
>function.(these unknow character is like "<<" or ">>", but my string doesn't
>include these characters.)
>
>could anybody tell me how to resolve it , thanks.
>
>Lorry


Re: How to convert from string to char*? by John

John
Thu Mar 01 16:27:37 CST 2007

"Ben" <Ben@discussions.microsoft.com> wrote in message
news:88CD22A7-B3E6-47B2-BA06-F8B031D54484@microsoft.com
> string::data()

data() also returns a read only string. The difference between data() and
c_str() is simply one of whether or not the string is nul-terminated.


--
John Carson