You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# `CString` Operations Relating to C-Style Strings
9
9
10
-
A [CString](../atl-mfc-shared/using-cstring.md) object contains character string data. `CString` inherits the set of the [methods and operators](../atl-mfc-shared/reference/cstringt-class.md) that are defined in the class template [CStringT](../atl-mfc-shared/reference/cstringt-class.md) to work with string data. (`CString` is a **`typedef`** that specializes `CStringT` to work with the kind of character data that `CString` supports.)
10
+
A [`CString`](../atl-mfc-shared/using-cstring.md) object contains character string data. `CString` inherits the set of the [methods and operators](../atl-mfc-shared/reference/cstringt-class.md) that are defined in the class template [`CStringT`](../atl-mfc-shared/reference/cstringt-class.md) to work with string data. (`CString` is a **`typedef`** that specializes `CStringT` to work with the kind of character data that `CString` supports.)
11
11
12
12
`CString` does not store character data internally as a C-style null-terminated string. Instead, `CString` tracks the length of character data so that it can more securely watch the data and the space it requires.
13
13
@@ -17,23 +17,23 @@ A [CString](../atl-mfc-shared/using-cstring.md) object contains character string
17
17
18
18
-[Working with standard run-time library string functions](#_core_working_with_standard_run.2d.time_library_string_functions)
## <aname="_core_using_cstring_as_a_c.2d.style_null.2d.terminated_string"></a> Using CString as a C-Style Null-Terminated String
26
+
## <aname="_core_using_cstring_as_a_c.2d.style_null.2d.terminated_string"></a> Using `CString` as a C-Style Null-Terminated String
27
27
28
-
To use a `CString` object as a C-style string, cast the object to LPCTSTR. In the following example, the `CString` returns a pointer to a read-only C-style null-terminated string. The `strcpy` function puts a copy of the C-style string in the variable `myString`.
28
+
To use a `CString` object as a C-style string, cast the object to `LPCTSTR`. In the following example, the `CString` returns a pointer to a read-only C-style null-terminated string. The `strcpy` function puts a copy of the C-style string in the variable `myString`.
29
29
30
30
```cpp
31
31
CString aCString = "A string";
32
32
char myString[256];
33
33
strcpy(myString, (LPCTSTR)aCString);
34
34
```
35
35
36
-
You can use `CString` methods, for example, `SetAt`, to modify individual characters in the string object. However, the LPCTSTR pointer is temporary and becomes invalid when any change is made to `CString`. The `CString` can also go out of scope and be automatically deleted. We recommend that you get a fresh LPCTSTR pointer of a `CString` object every time that you use one.
36
+
You can use `CString` methods, for example, `SetAt`, to modify individual characters in the string object. However, the `LPCTSTR` pointer is temporary and becomes invalid when any change is made to `CString`. The `CString` can also go out of scope and be automatically deleted. We recommend that you get a fresh `LPCTSTR` pointer of a `CString` object every time that you use one.
37
37
38
38
Sometimes you may require a copy of `CString` data to modify directly. Use the more secured function `strcpy_s` (or the Unicode/MBCS-portable `_tcscpy_s`) to copy the `CString` object into a separate buffer. This is where characters can be safely modified, as shown by the following example.
39
39
@@ -46,35 +46,35 @@ Sometimes you may require a copy of `CString` data to modify directly. Use the m
46
46
47
47
You should be able to find a `CString` method to perform any string operation for which you might consider using the standard C run-time library string functions such as `strcmp` (or the Unicode/MBCS-portable `_tcscmp`).
48
48
49
-
If you must use the C run-time string functions, you can use the techniques described in _core_using_cstring_as_a_c.2d.style_null.2d.terminated_string. You can 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.
49
+
If you must use the C run-time string functions, you can use the techniques described in [Using `CString` as a C-style null-terminated string](#_core_using_cstring_as_a_c.2d.style_null.2d.terminated_string). You can 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.
In most situations, you should use `CString` member functions to modify the contents of a `CString` object or to convert the `CString` to a C-style character string.
54
54
55
55
There are some situations where it makes sense to directly modify the `CString` contents, for example, when you work with operating-system functions that require a character buffer.
56
56
57
57
The `GetBuffer` and `ReleaseBuffer` methods offer access to the internal character buffer of a `CString` object and let you modify it directly. The following steps show how to use these functions for this purpose.
58
58
59
-
### To use GetBuffer and ReleaseBuffer to access the internal character buffer of a CString object
59
+
### To use `GetBuffer` and `ReleaseBuffer` to access the internal character buffer of a `CString` object
60
60
61
61
1. Call `GetBuffer` for a `CString` object and specify the length of the buffer you require.
62
62
63
63
1. Use the pointer returned by `GetBuffer` to write characters directly into the `CString` object.
64
64
65
65
1. Call `ReleaseBuffer` for the `CString` object to update all the internal `CString` state information, for example, the length of the string. After you modify the contents of a `CString` object directly, you must call `ReleaseBuffer` before you call any other `CString` member functions.
66
66
67
-
## <a name="_core_using_cstring_objects_with_variable_argument_functions"></a> Using CString Objects with Variable Argument Functions
67
+
## <a name="_core_using_cstring_objects_with_variable_argument_functions"></a> Using `CString` Objects with Variable Argument Functions
68
68
69
69
Some C functions take a variable number of arguments. A notable example is `printf_s`. 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 each 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.
70
70
71
-
To use a `CString` object in a variable argument function, explicitly cast the `CString` to an LPCTSTR string, as shown in the following example.
71
+
To use a `CString` object in a variable argument function, explicitly cast the `CString` to an `LPCTSTR` string, as shown in the following example.
For most functions that need a string argument, it is best to specify the formal parameter in the function prototype as a **`const`** pointer to a character (`LPCTSTR`) instead of a `CString`. When a formal parameter is specified as a **`const`** pointer to a character, you can pass either a pointer to a TCHAR array, a literal string [`"hi there"`], or a `CString` object. The `CString` object will be automatically converted to an LPCTSTR. Any place you can use an LPCTSTR, you can also use a `CString` object.
77
+
For most functions that need a string argument, it is best to specify the formal parameter in the function prototype as a **`const`** pointer to a character (`LPCTSTR`) instead of a `CString`. When a formal parameter is specified as a **`const`** pointer to a character, you can pass either a pointer to a `TCHAR` array, a literal string [`"hi there"`], or a `CString` object. The `CString` object will be automatically converted to an `LPCTSTR`. Any place you can use an `LPCTSTR`, you can also use a `CString` object.
78
78
79
79
You can also specify a formal parameter as a constant string reference (that is, `const CString&`) if the argument will not be modified. Drop the **`const`** modifier if the string will be modified by the function. If a default null value is desired, initialize it to the null string [`""`], as shown below:
80
80
@@ -85,4 +85,4 @@ For most function results, you can simply return a `CString` object by value.
0 commit comments