strcat-s

Add string to string in C with safe functions

风格不统一 提交于 2020-01-03 17:15:50
问题 I want to copy the a file name to a string and append ".cpt" to it. But I am unable to do this with safe functions (strcat_s). Error: "String is not null terminated!". And I did set '\0', how to fix this using safe functions? size = strlen(locatie); size++; nieuw = (char*)malloc(size+4); strcpy_s(nieuw, size, locatie); nieuw[size] = '\0'; strcat_s(nieuw, 4, ".cpt"); // <-- crash puts(nieuw); 回答1: The size parameter of the _s functions is the size of the destination buffer, not the source. The

Why does MSVC++ consider “std::strcat” to be “unsafe”? (C++)

对着背影说爱祢 提交于 2019-11-29 14:20:33
When I try to do things like this: char* prefix = "Sector_Data\\sector"; char* s_num = "0"; std::strcat(prefix, s_num); std::strcat(prefix, "\\"); and so on and so forth, I get a warning warning C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. Why is strcat considered unsafe, and is there a way to get rid of this warning without using strcat_s? Also, if the only way to get rid of the warning is to use strcat_s, how does it work (syntax-wise: apparently it does not take two arguments). Because the buffer, prefix, could have less space than you are