Strcpy_s__jquery of C + + security functions

Source: Internet
Author: User
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

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.