c++ - How to avoid strcpy_s buffer too small -


I have to copy 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   

How can I always copy the length of the array safely?

Should I like this every time?

  std :: string to copy = data.substr (0, size-1); Strcpy_s (name, toCopy.c_str ());    

_TRUNCATE

  strncpy_s (name, data.c_str (), _TRUNCATE);   

Anyone can copy it to fill the name buffer while still keeping in mind the termination termination (unlike conventional struncup ).

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -