Operations Related to C-Style Strings

It is often useful to be able to manipulate the contents of a CString object as if it were a C-style null-terminated string.

·To convert to C-style null-terminated strings:

In the simplest case, you can cast a CString object to be a pointer to const char. The const char* type conversion operator returns a read-only pointer to a C-style null-terminated string from a CString object.

The pointer to char returned by the implicit conversion shown above points into the data area used by the CString. If the CString goes out of scope and is automatically deleted or something else changes the contents of the CString, the char pointer will no longer be valid. You should treat this pointer as a temporary read-only pointer. Do not directly modify the characters pointed to.

You can use CString functions such as SetAt to modify individual characters in the string object, but if you need a copy of a CString object's characters that you can modify directly, use strcpy to copy the CString object into a separate buffer where the characters can be safely modified, as shown by the following example:

CString theString( "This is a test" );

char* psz = new char[theString.GetLength()];

strcpy( psz,theString );

//... modify psz as much as you want

Note:

The second argument to strcpy is declared as a constant pointer to char (const char*). The example above passes a CString for this argument. The C++ compiler automatically applies the conversion function defined for the CString class that converts a CString to a const char*. The ability to define casting operations from one type to another is one of C++'s most useful features.

·To work with standard C-library string functions:

In most situations, you should be able to find CString member functions to perform any string operation for which you might consider using the standard C run-time library string functions, such as strcmp.

If you find that you must use the C run-time string functions, you can use the techniques described in the previous procedure to copy the CString object to an equivalent C-style string buffer, perform your operations on the buffer, and then assign the resulting C-style string back to a CString object.

·To modify CString contents directly with GetBuffer and ReleaseBuffer:

In most situations, you should use CString member functions to modify the contents of a CString object, or convert the CString to a C-style character string as described in the pervious section.

However, there are certain situations, such as working with operating system functions that require a character buffer, where it is advantageous to directly modify the CString contents.

The GetBuffer and ReleaseBuffer member functions allow you to gain access to the internal character buffer of a CString and modify it directly. The following steps show how to use these functions for this purpose.

1.Call GetBuffer for a CString object, specifying the length of the buffer your require.

2.Use the pointer returned by GetBuffer to write characters directly into the CString object.

3.Call ReleaseBuffer for the CString object to update all the internal CString state information (such as the length of the string). After modifying a CString object's contents directly, you must call ReleaseBuffer before calling any other CString member functions.

·To use CString objects with variable argument functions:

Some C functions take a variable number of arguments. A notable example is printf. Because of the way this kind of function is declared, the compiler cannot be sure of the type of the arguments and cannot determine which conversion operation to perform on the argument. Therefore, it is essential that you use an explicit type cast when passing a CString object to a function that takes a variable number of arguments.

Explicitly cast the CString to a pointer to a constant char string, as shown here:

CString kindOfFruit = "bananas";

int howmany = 25;

printf( "You have %d %s\n", howmany, (const char*)kindOfFruit );