📄 faq40.htm
字号:
<HTML>
<HEAD>
<TITLE>Convert an AnsiString to a char *</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3>
Convert an AnsiString to a char *
</H3>
<P>
Use the <TT>c_str()</TT> member function of <TT>AnsiString</TT>. Here are some examples.
</P>
<pre>
<font color="navy">// Passing an AnsiString to a message box</font>
AnsiString str <b>=</b> <font color="blue">"Hello Dr. Crane"</font><b>;</b>
Application<b>-></b>MessageBox<b>(</b>str<b>.</b>c_str<b>(</b><b>)</b><b>,</b> <font color="blue">"AnsiString to char *"</font><b>,</b> MB_OK<b>)</b><b>;</b>
<font color="navy">// Passing an AnsiString to an API function</font>
<b>:</b><b>:</b>DrawText<b>(</b>Canvas<b>-></b>Handle<b>,</b> str<b>.</b>c_str<b>(</b><b>)</b><b>,</b> str<b>.</b>Length<b>(</b><b>)</b><b>,</b> rect<b>,</b> DT_VCENTER<b>)</b><b>;</b>
</pre>
<P>
<B>Note 1: </B><TT>c_str</TT> is an inline member function of the <TT>AnsiString</TT>
class. The code for <TT>c_str()</TT> reveals that the function returns the pointer to
<TT>AnsiString</TT>'s internal <TT>char *</TT> buffer.
<pre>
<font color="navy">// Data is a char * member of AnsiString</font>
<b>char</b><b>*</b> <b>__fastcall</b> c_str<b>(</b><b>)</b> <b>const</b>
<b>{</b><b>return</b> <b>(</b>Data<b>)</b><b>?</b> Data<b>:</b> <font color="blue">""</font><b>;</b><b>}</b>
</pre>
<P>
This code is equivalent to:
</P>
<pre>
<b>char</b><b>*</b> <b>__fastcall</b> AnsiString<b>:</b><b>:</b>c_str<b>(</b><b>)</b> <b>const</b>
<b>{</b>
<b>if</b><b>(</b>Data <b>!=</b> NULL<b>)</b>
<b>return</b> <b>(</b>Data<b>)</b><b>;</b>
<b>else</b>
<b>return</b> <font color="blue">""</font><b>;</b>
<b>}</b>
</pre>
<B>Note 2: </B>Because <TT>c_str</TT> returns a non-const pointer to the internal buffer, you
can use <TT>c_str</TT> to modify an <TT>AnsiString</TT> variable. Here are some examples.
<pre>
<font color="navy">// This example creates an AnsiString and fills it with zeros</font>
AnsiString str<b>;</b>
str<b>.</b>SetLength<b>(</b><font color="blue">50</font><b>)</b><b>;</b> <font color="navy">// allocate space for 50 characters</font>
memset <b>(</b>str<b>.</b>c_str<b>(</b><b>)</b><b>,</b> <font color="blue">0</font><b>,</b><font color="blue">50</font><b>)</b><b>;</b> <font color="navy">// fill buffer with zeroes</font>
<font color="navy">// This example passes an AnsiString to an API function that wants to</font>
<font color="navy">// write to the string.</font>
AnsiString str<b>;</b>
DWORD size <b>=</b> <font color="blue">255</font><b>;</b>
str<b>.</b>SetLength<b>(</b>size <b>+</b><font color="blue">1</font><b>)</b><b>;</b> <font color="navy">// 255 characters plus null term</font>
GetComputerName<b>(</b>str<b>.</b>c_str<b>(</b><b>)</b><b>,</b> <b>&</b>size<b>)</b> <b>;</b> <font color="navy">// call the API Get functions</font>
</pre>
<P>
Note that just because you can write to the pointer returned by
<TT>c_str()</TT>, this does not mean that you actually should. Modifying the
internal data of an object is usually a bad idea. In fact, the <TT>std::string</TT>
class does not allow you to write to the pointer returned by its <TT>c_str</TT>
member function. Some people believe that <TT>AnsiString</TT> should also forbid it.
Try to avoid writing to the <TT>c_str()</TT> buffer if at all possible
</P>
<P>
Also keep in mind that when you write to <TT>c_str()</TT>, that the value returned
from the <TT>Length</TT> member function will probably not equal the string length.
It will equal the value that you passed to <TT>SetLength</TT>. You must also be
careful not to overrun the buffer.
</P>
<P>
<B>Note 3: </B>Note 1 shows that <TT>c_str</TT> is a constant member function of
the <TT>AnsiString</TT> class. This means that you can call <TT>c_str</TT> on
constant <TT>AnsiString</TT> variables. C++ does not allow you to call non-const
functions on constant objects. The following code would not compile
if Borland had omitted the <TT>const</TT> keyword in the declaration of the
<TT>c_str</TT> method.</P>
<pre>
<b>const</b> AnsiString str <b>=</b> <font color="blue">"Hello Dr Crane"</font><b>;</b>
Application<b>-></b>MessageBox<b>(</b>str<b>.</b>c_str<b>(</b><b>)</b><b>,</b> <font color="blue">""</font><b>,</b>MB_OK<b>)</b><b>;</b> <font color="navy">// error, calling non-const</font>
<font color="navy">// method on const object</font>
</pre>
<P>
<B>Note 4: </B>You will almost always call the <TT>c_str</TT> function using the
dot "." notation rather than the pointer notation <TT>"->"</TT>. This also applies to
<TT>AnsiString</TT> properties of VCL components. When a VCL component has an
<TT>AnsiString</TT> property, the Get method for the property returns a new
<TT>AnsiString</TT> object by value. Since the object is returned by value, you
dereference its member functions using the dot notation.
</P>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -