CString Operations Guide

Source: Internet
Author: User
Tags requires strlen

By reading this article you can learn how to use CString effectively.

CString is a very useful type of data. They largely simplify many of the operations in MFC, making it much easier for MFC to do string manipulation. In any case, there are a lot of special techniques for using CString, especially for programmers who come out of the pure C background. This article will discuss these techniques.

Using CString allows you to manipulate strings more directly. This article is not a complete manual for CString, but encompasses most of the common basic questions.

I'll discuss the following separately.

1, the CString object connection

One aspect of CString type convenience features is the connection of strings, using the CString type, you can easily connect two strings, as in the following example:

CString gray("Gray");
CString cat("Cat");
CString graycat = gray + cat;

is much better than the following method:

char gray[] = "Gray";
char cat[] = "Cat";
char * graycat = malloc(strlen(gray) + strlen(cat) + 1);
strcpy(graycat, gray);
strcat(graycat, cat);

2. Format string

Rather than using the sprintf () function or the wsprintf () function to format a string, you might as well use the format () method of the CString object:

CString s;
s.Format(_T("The total is %d"), total);

The advantage of this approach is that you don't have to worry about whether the buffer used to store the formatted data is large enough for you to do with the CString class.

Formatting is one of the most common techniques for converting other data that is not a string type into a CString type, such as converting an integer to a CString type, using the following methods:

CString s;
s.Format(_T("%d"), total);

I always use the _t () macro for my string, which is to make my code at least Unicode aware, and of course, the topic of Unicode is not covered in this article. The _t () macro is defined as follows in a 8-bit character environment:

#define _T(x) x // 非Unicode版本(non-Unicode version)

The Unicode environment is defined as follows:

#define _T(x) L##x // Unicode版本(Unicode version)

So in a Unicode environment, the effect is equivalent to:

s.Format(L"%d", total);

If you think your program may be running in Unicode, then start to care about Unicode encoding. For example, do not use the sizeof () operator to get the length of the string, because there will be twice times the error in the Unicode environment. There are some ways to hide some of the details of Unicode, such as when I need to get the length of a character, I use a macro called dim, which is defined in my dim.h file, which I will include in all the programs I write:

#define DIM(x) ( sizeof((x)) / sizeof((x)[0]) )

This macro can be used not only to solve the string length of Unicode, but also to use the table defined at compile time to get the number of entries in the table as follows:

class Whatever { ... };
Whatever data[] = {
  { ... },
  { ... },
for(int i = 0; i < DIM(data); i++) // 扫描表格寻找匹配项。

Here to remind you that you must pay attention to those in the parameters of the actual number of bytes of the API function call, if you pass the number of characters to it, it will not work correctly. As follows:

TCHAR data[20];
lstrcpyn(data, longstring, sizeof(data) - 1); // WRONG!
lstrcpyn(data, longstring, DIM(data) - 1); // RIGHT
WriteFile(f, data, DIM(data), &bytesWritten, NULL); // WRONG!
WriteFile(f, data, sizeof(data), &bytesWritten, NULL); // RIGHT

The reason for this is that Lstrcpyn requires a number of characters as an argument, but WriteFile requires a byte number as an argument.

It is also important to note that there are times when you need to write down all the content of the data. If you just want to write the true length of the data, you might think you should:

WriteFile(f, data, lstrlen(data), &bytesWritten, NULL); // WRONG

However, in a Unicode environment, it does not work properly. The right approach should be this:

WriteFile(f, data, lstrlen(data) * sizeof(TCHAR), &bytesWritten, NULL); // RIGHT

Because WriteFile requires a length in bytes. (Perhaps some people would like to "run this line of code in a non-Unicode environment, which means that you're always doing an extra 1 operation, so it doesn't degrade the efficiency of the program?") "This idea is redundant, you have to understand what the compiler actually does, and no C or C + + compiler will leave this tedious 1 operation in the code." When running in a Unicode environment, you don't have to worry that the 2 operation will reduce the efficiency of the program, remember, this is just a left-shift operation, and the compiler is happy to make this replacement for you. )

Using the _t macro doesn't mean you've created a Unicode program, you just created a Unicode-aware program. If you compile your program in the default 8-bit mode, you will get an ordinary 8-bit application (where 8-bit refers to a 8-bit character encoding, not a 8-bit computer system); When you compile your program in a Unicode environment, You're going to get a Unicode program. Remember, CString is a Unicode environment that contains 16-bit characters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.