There are certain cases when I really need the strncpy()funcitonalty function - for example, I have a function in a predefined interface to which the buffer address and buffer size are passed:
HRESULT someFunction( char* buffer, size_t length );
and it’s documented that I can copy a null-terminated string with a length of no more length- if it is exactly equal to the length lengthI am not null-terminated, and the caller knows that the string ends with either a null character or a length length, whichever happens first and it all works.
Of course I will use strncpy()for this
HRESULT someFunction( char* buffer, size_t length )
{
const char* toCopy = ...
size_t actualLength = strlen( toCopy );
if( actualLength > length ) {
return E_UNEXPECTED;
}
strncpy( buffer, toCopy, length );
return S_OK;
}
Visual ++ 7 Visual ++ 9. . , strncpy() , strncpy_s().
strncpy_s() , - , . E_UNEXPECTED , length - 1 ( length, ), , length , undefined.
, , - _CRT_SECURE_NO_WARNINGS .
strncpy_s() strncpy()?