c++ - How to avoid strcpy_s buffer too small -
I have to copy the How can I always copy the length of the array safely? Should I like this every time? Anyone can copy it to fill the std :: string data into four arrays. The length of my string is variable, but the length of my four array is fixed.
const int = 5; Four names [size]; Std :: string data = "1234567890"; Strcpy_s (name, 5, data.c_str ()); // buffer causes buffer too small claims strcpy_s (name, 11, data.c_str ()); // copies fine (data length and zero) strcpy_s (name, size (data), data.c_str ()); // copy is ok
std :: string to copy = data.substr (0, size-1); Strcpy_s (name, toCopy.c_str ());
_TRUNCATE
strncpy_s (name, data.c_str (), _TRUNCATE);
name buffer while still keeping in mind the termination termination (unlike conventional struncup ).
Comments
Post a Comment