Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need a example for strcat_s()

Tags:

c++

Can you please give me an example for c++. which works on Visual studio 2019.

I tried this

  char test_name[50];
    char SQLQueryViewMARKS_12A[] = "SELECT *FROM Marks_12A WHERE Test_Name = ";
    cin>>test_name;
    strcat_s(SQLQueryViewMARKS_12A,sizeof(SQLQueryViewMARKS_12A),"'/0 ");
    strcat_s(SQLQueryViewMARKS_12A,sizeof(SQLQueryViewMARKS_12A),test_name);
    strcat_s(SQLQueryViewMARKS_12A,sizeof(SQLQueryViewMARKS_12A), "'/0");

The expected result was :

If user entered UT

The query must get concatenate to

char SQLQueryViewMARKS_12A[] = "SELECT *FROM Marks_12A WHERE Test_Name = ' UT ' ";
like image 351
Tejas Avatar asked May 30 '26 01:05

Tejas


1 Answers

Your code is almost right. The only thing that is missing is the space for the additional string that you would like concatenated to the original:

char SQLQueryViewMARKS_12A[128] = "SELECT *FROM Marks_12A WHERE Test_Name = ";
//                         ^^^
// Give your string more space to ensure that additional characters have place to fit
//
strcat_s(SQLQueryViewMARKS_12A,sizeof(SQLQueryViewMARKS_12A),"'/0 ");

Note that although strcat_s works fine in C++, std::string from the Standard C++ library gives you a much more powerful alternative:

std::string SQLQueryViewMARKS_12A = "SELECT *FROM Marks_12A WHERE Test_Name = ";
SQLQueryViewMARKS_12A += "'/0 ";
like image 176
Sergey Kalinichenko Avatar answered Jun 01 '26 15:06

Sergey Kalinichenko