C++ strcpy_s和strncpy_s使用方法

strcpy_s

函数说明

1、头文件
#include <string.h>
2、函数声明
errno_t __cdecl strcpy_s(
        _Out_writes_z_(_SizeInBytes) char*       _Destination,
        _In_                         rsize_t     _SizeInBytes,
        _In_z_                       char const* _Source
        );
3、函数原型

3.1 三个参数

strcpy_s( char *strDestination, size_t numberOfElements, const char *strSource );

功能: 复制 strSource 的全部字符串内容到 strDestination 中。
strDestination 的容器大小必须大等于 strSource 的字符串大小,numberOfElements 大小为 strSource 的大小或大于 strSource 小等于 strDestination 。

3.2 两个参数

strcpy_s( char (&strDestination)[size], const char *strSource ); 

功能: 复制 strSource 的全部字符串内容到 strDestination 中。
strDestination 的容器大小必须大等于 strSource 的字符串大小。

注意: 该用法不规范,strDestination 为数值类型时可以正常使用,strDestination 为 new 动态分配的内存时无法使用。

示例说明

三个参数

程序1:

#include <iostream>
using namespace std;

int main() {
	const char* source = "123456789";
	char dest[20];
	strcpy_s(dest,10, source);//numberOfElements等于字符串大小
	cout << dest<<"*";
	return 0;
}

输出:
在这里插入图片描述

程序2:

#include <iostream>
using namespace std;

int main() {
	const char* source = "123456789";
	char dest[20];
	strcpy_s(dest,15, source);//numberOfElements大于字符串大小
	cout << dest<<"*";
	return 0;
}

输出:
在这里插入图片描述

两个参数

程序1:

目标内存使用数组类型

#include <iostream>
using namespace std;

int main() {
	const char* source = "123456789";
	char dest[20];
	strcpy_s(dest,source);
	cout << dest<<"*";
	return 0;
}

输出:
在这里插入图片描述

程序2:

#include <iostream>
using namespace std;

int main() {
	const char* source = "123456789";
	char* dest = new char[20];
	strcpy_s(dest,source);//出错,无法进行编译
	cout << dest<<"*";
	return 0;
}

strncpy_s

函数说明

1、头文件
#include <string.h>
2、函数声明
strncpy_s(
        _Out_writes_z_(_SizeInBytes) char*       _Destination,
        _In_                         rsize_t     _SizeInBytes,
        _In_reads_or_z_(_MaxCount)   char const* _Source,
        _In_                         rsize_t     _MaxCount
        );
3、函数原型
strncpy_s(char * dest, int destSize, char * source, int count);

功能:
从 source 处复制 count 个字符到 dest 处。参数2为目标缓冲区大小,参数4为欲拷贝的字符数目。函数功能可以理解为从 source 中拷贝 count 个字符到 dest 中,拷贝前 dest 中的内容会被清空。

注意:

  • 在从 source 中拷贝 count 个字符的过程中,如果遇到空字符 ‘\0’ ,则停止拷贝后面的字符,即使字符数目没有达到 count 。
  • count 大小 必须 小于 destSize 大小 ,不能等于 destSize 大小。在无法顺利读取到 count 个字符时 destSize 的数值可以小等于 count 的数值,且不会报错,但这样操作不安全。
  • destSize 大小可以小于 dest 所申请的缓存区大小。

示例说明

程序1:

如果遇到空字符 ‘\0’ ,则停止拷贝后面的字符

#include <iostream>
using namespace std;

int main() {
	char source[7] = {'1','2','3','4','\0','6','7'};
	char* dest = new char[20]{};
	strncpy_s(dest,20 ,source,7);//提示可能没有为source添加字符串零终止符
	cout << dest << "*" << endl;
	return 0;
}

输出:
在这里插入图片描述

程序2:

如果遇到空字符 ‘\0’ ,则停止拷贝后面的字符

#include <iostream>
using namespace std;

int main() {
	const char* source = "1234'\0'567";
	char* dest = new char[20]{};
	strncpy_s(dest,20 ,source,10);
	cout << dest << "*" << endl;
	return 0;
}

输出:
在这里插入图片描述

程序3:

count 大小 必须 小于 destSize 大小 ,不能等于 destSize 大小。在无法顺利读取到 count 个字符时 destSize 的数值可以小等于 count 的数值,且不会报错,但这样操作不安全。

#include <iostream>
using namespace std;

int main() {
	const char* source = "1234'\0'678910";
	char* dest = new char[20]{};
	strncpy_s(dest,9 ,source,10);
	cout << dest << "*" << endl;
	return 0;
}

输出:
在这里插入图片描述

  • 7
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值