Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 10: Characteristics of a Window's Frame


string Producer;

Producer.assign(Artist);

int Len = Producer.length();

pDC->TextOut(20, 20, Producer.c_str(), Len);
}

9.2.2 String Concatenation...........................................................................


String concatenation consists of adding one string to another. To add one null-terminated
string to another, you can use the strcat() function. Its syntax is:

char *strcat(char *Destination, const char *Source);

The strcat() function takes two arguments. The second argument, Source, is the string to
add to the right side of the first string, Destination. Here is an example:

void CExerciseDlg::OnMsgBox()
{
// TODO: Add your control notification handler code here
static char Msg[] = "This application has performed an illegal operation ";
strcat(Msg, "and it will be shut down");
strcat(Msg, "\nDo you want to save the last changes you made to the current file?");
const char Title[] = "Application Global Error";
UINT Option = MB_YESNO | MB_ICONSTOP;

::MessageBox(NULL, Msg, Title, Option);
}

Like the strcat() function, the strncat() function is used to append one string to another.
The difference is that, while the strcat() considers all characters of the source string, the
strncat() function allows you to specify the number of characters from the source string
that you want to append to the destination string. This means that, if the source string has
12 characters, you can decide to append only a set number of its characters. The syntax of
the strncat() function is:

char* strncat(char* Destination, const char* Source, int Number);

Besides the same arguments as the strcat() function, the Number argument is used to
specify the number of characters considered from Source. To perform the concatenation,
the compiler would count Number characters from left to right of the Source string. These
characters would be added to the right of the Destination string. Here is an example:

void CExerciseView::OnDraw(CDC* pDC)
{
Free download pdf