fork download
#include <string>
#include <stdio.h>
#include <string.h>
#include <cassert>

inline void CopyAndNullTerminate( const std::string& source, char* dest, size_t dest_size )
{
    source.copy( dest, dest_size );
    using std::min;
    char* end = dest + min( static_cast< size_t >( dest_size - 1 ), source.size() );
    *end = '\0';
}

int main()
{
    enum{ MAX_STRING_SIZE = 15 };
    char dest[ MAX_STRING_SIZE ];
    std::string test = "This is a test";

    CopyAndNullTerminate( test, dest, MAX_STRING_SIZE );

    assert( strcmp( test.c_str(), dest ) == 0 );
    return 0;
}
Success #stdin #stdout 0.01s 2808KB
stdin
Standard input is empty
stdout

Standard output is empty