Skip to content

Repo sync for protected CLA branch #3173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
be21eab
initial
Apr 28, 2021
01e2b15
initial
Apr 29, 2021
3afa51c
Merge branch 'master' into code-escape-pr15
May 4, 2021
7751379
Merge branch 'master' into code-escape-pr14
May 4, 2021
9fb0060
adjustments
May 7, 2021
a65d184
merge
May 7, 2021
546028a
adjustments
May 10, 2021
f2e8acc
adjustments
May 10, 2021
36d7ca5
Update crect-class.md
May 11, 2021
5cf7bfd
adjustments
May 11, 2021
1a88104
adjustments
May 18, 2021
cb6acc1
merge
May 18, 2021
86f412a
adjustment
May 19, 2021
9d6ac50
adjustment
May 19, 2021
27362c1
Update cstring-operations-relating-to-c-style-strings.md
May 19, 2021
dfa35ec
Work in progress on DesktopCompatible
May 20, 2021
789adc7
Update casyncsocket-class.md
May 26, 2021
9c4d86e
Update ctreectrl-class.md
May 26, 2021
c60779f
Update cstdiofile-class.md
May 26, 2021
858dda3
Update casyncsocket-class.md
May 27, 2021
137543c
Update ctreectrl-class.md
May 27, 2021
127817c
Update cmenu-class.md
May 27, 2021
55a8ecf
Update ctreectrl-class.md
May 27, 2021
1efe322
Merge pull request #3544 from msebolt/code-escape-pr15
v-shils May 27, 2021
6429ca9
Update styles-used-by-mfc.md
jborsecnik May 27, 2021
85191bc
Merge pull request #3543 from msebolt/code-escape-pr14
jborsecnik May 27, 2021
d300d72
Code escape pr13 (#3533)
May 27, 2021
30fbb52
Merge pull request #3592 from MicrosoftDocs/master
Taojunshen May 28, 2021
11cc2f2
Update General property page for 16.10
May 29, 2021
1a859cc
Merge pull request #3593 from corob-msft/docs/corob/DesktopCompatible
ttorble Jun 1, 2021
df24fc4
Merge pull request #3594 from MicrosoftDocs/master
Taojunshen Jun 1, 2021
1e8960a
Merging changes synced from https://github.com/MicrosoftDocs/cpp-docs…
opbld17 Jun 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ ms.date: "11/04/2016"
helpviewer_keywords: ["CString objects, basic operations", "MFC [C++], string handling class", "string conversion [C++], C-style strings", "strings [C++], string operations", "standard run-time library string functions", "null values, Null-terminated string conversion", "string functions", "strings [C++], in C", "string arguments", "C-style strings", "strings [C++], class CString", "casting CString objects"]
ms.assetid: 5048de8a-5298-4891-b8a0-c554b5a3ac1b
---
# CString Operations Relating to C-Style Strings
# `CString` Operations Relating to C-Style Strings

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.)
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.)

`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.

Expand All @@ -17,23 +17,23 @@ A [CString](../atl-mfc-shared/using-cstring.md) object contains character string

- [Working with standard run-time library string functions](#_core_working_with_standard_run.2d.time_library_string_functions)

- [Modifying CString contents directly](#_core_modifying_cstring_contents_directly)
- [Modifying `CString` contents directly](#_core_modifying_cstring_contents_directly)

- [Using CString objects with variable argument functions](#_core_using_cstring_objects_with_variable_argument_functions)
- [Using `CString` objects with variable argument functions](#_core_using_cstring_objects_with_variable_argument_functions)

- [Specifying CString formal parameters](#_core_specifying_cstring_formal_parameters)
- [Specifying `CString` formal parameters](#_core_specifying_cstring_formal_parameters)

## <a name="_core_using_cstring_as_a_c.2d.style_null.2d.terminated_string"></a> Using CString as a C-Style Null-Terminated String
## <a name="_core_using_cstring_as_a_c.2d.style_null.2d.terminated_string"></a> Using `CString` as a C-Style Null-Terminated String

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`.
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`.

```cpp
CString aCString = "A string";
char myString[256];
strcpy(myString, (LPCTSTR)aCString);
```

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.
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.

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.

Expand All @@ -46,35 +46,35 @@ Sometimes you may require a copy of `CString` data to modify directly. Use the m

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`).

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.
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.

## <a name="_core_modifying_cstring_contents_directly"></a> Modifying CString Contents Directly
## <a name="_core_modifying_cstring_contents_directly"></a> Modifying `CString` Contents Directly

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.

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.

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.

### To use GetBuffer and ReleaseBuffer to access the internal character buffer of a CString object
### To use `GetBuffer` and `ReleaseBuffer` to access the internal character buffer of a `CString` object

1. Call `GetBuffer` for a `CString` object and specify the length of the buffer you require.

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

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.

## <a name="_core_using_cstring_objects_with_variable_argument_functions"></a> Using CString Objects with Variable Argument Functions
## <a name="_core_using_cstring_objects_with_variable_argument_functions"></a> Using `CString` Objects with Variable Argument Functions

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.

To use a `CString` object in a variable argument function, explicitly cast the `CString` to an LPCTSTR string, as shown in the following example.
To use a `CString` object in a variable argument function, explicitly cast the `CString` to an `LPCTSTR` string, as shown in the following example.

[!code-cpp[NVC_ATLMFC_Utilities#190](../atl-mfc-shared/codesnippet/cpp/cstring-operations-relating-to-c-style-strings_2.cpp)]

## <a name="_core_specifying_cstring_formal_parameters"></a> Specifying CString Formal Parameters
## <a name="_core_specifying_cstring_formal_parameters"></a> Specifying `CString` Formal Parameters

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.
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.

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:

Expand All @@ -85,4 +85,4 @@ For most function results, you can simply return a `CString` object by value.
## See also

[Strings (ATL/MFC)](../atl-mfc-shared/strings-atl-mfc.md)<br/>
[CString Argument Passing](../atl-mfc-shared/cstring-argument-passing.md)
[`CString` Argument Passing](../atl-mfc-shared/cstring-argument-passing.md)
Loading