C + + security function strcpy_s 1. Must include the header file:<string.h> 2. function declaration:
errno_t strcat_s (
char *strdestination,
size_t numberofelements,
const char *strsource
3. Parameter Introduction
Strdestination The position of the destination string buffer. numberOfElements The size of the target string buffer in the multibyte narrow function char unit and the wide function wchar_t cell. Strsource A null-terminated source string buffer. |
4. Examples of Use
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
int main (void)
{
char string[80];
Using template versions of strcpy_s and strcat_s:
strcpy_s (String, "Hello World");
strcat_s (String, "strcpy_s");
strcat_s (String, "and");
Of course we can supply the size explicitly if we want to:
strcat_s (String, _countof (String), "strcat_s!");
printf ("string =%s\n", string);
System ("Pause");
}
5. Note In the use of strcpy_s, the use of two parameters to pay special attention to the point is that two parameters but if: char *str=new char[7]; ERROR: Hint does not support two parameters. The new string, which prompts for two parameters, must be three arguments.
#include <iostream>
#include <string.h>
using namespace std;
void Test (void)
{
char *str1 = NULL;
STR1 = new CHAR[20];
Char str[7];
strcpy_s (str1, "Hello World");//Three Parameters
strcpy_s (str, "Hello");//two parameters but if: char *str=new char[7]; Error: Prompt does not support two parameters
cout << "strlen (str1):" <<strlen (str1) <<endl<< "strlen (str):" <<strlen (str) < <endl;
cout << str1 << Endl;
cout << str << endl;
}
int main ()
{
Test ();
System ("Pause");
return 0;
}
The output is:
Strlen (STR1): 11//Note also: strlen (STR1) is the length of the computed string, excluding the "" at the end of the string!!!
Strlen (str): 5
Hello World
Hello