strcat_s和strcpy_s的用法
c++vs2017用到cstring库函数的时候老是出现这个错误
但能运行成功,网上查了半天没解决了,知道看到添加链接描述这位百度文库的文章”**试图得义“**大佬的讲解才明白点了。
strcpy_s定义
strcpy_s的参数(char *strDestination,size_t numberOfEl ements,char *strSource);
参数含义:
- strDestination 代表的是 目标字符串缓冲区的位置。
- numberOEIements代表的是 目标字符串缓冲区大小(允许范围内可省略)。
- strSource代表的是 Null 种植的源字符串缓冲区。
#include<iostream>
#include<cstring>
using namespace std;
void main() {
char dest[30], source[10];
for (int i=0;i<9;i++)
{
source[i] = 'a';
}
source[9] = 0;
strcat_s(dest, 30, source);
cout << dest << endl;
system("pause");
}
代码是上面大佬的运行结果是:aaaaaaaaa。
strcat_s的定义
strcat_s的参数是(char *strDestination,size_s munberOFEIements,char *strSource);
参数含义:
- strDestination : Null 终止的目标字符缓冲区。
- munberOFEIements:目标字符串缓冲区的大小(允许范围内可省略)。
- strSource:Null 终止的源字符串缓冲区。
include<iostream>
#include<cstring>
using namespace std;
void main() {
char dest[30], source1[10], source2[15];
for (int i=0;i<9;i++)
{
source1[i] = 'a';
}
source1[9] = 0;
strcpy_s(dest, 30, source1);
cout << dest << endl;
for (int i = 0; i < 15; i++)
{
source2[i] = 'b';
}
source2[14] = 0;
strcat_s(dest, 30, source2);
cout << dest << endl;
system("pause");
}
运行结果是aaaaaaaaa
aaaaaaaaabbbbbbbbbbbbbb
下面的是我的综合运用代码
#include<iostream>
#include<cstring>
using namespace std;
int main() {
char FirstName[10];
char LastName[10];
char Combin[20];
cout << "Enter your First name: ";
cin.getline(FirstName, 10);
cout << "Enter your Last name: ";
cin.getline(LastName, 10);
strcpy_s(Combin,20, LastName);
strcat_s(Combin,20, ", ");
strcat_s(Combin,20, FirstName);
cout << "Here's the information in a single string: " << Combin << endl;
system("pause");
return 0;
运行结果:
Enter your First name: aa
Enter your Last name: bb
Here’s the information in a single string: bb, aa
欧克