fork download
  1. #include <string>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <cassert>
  5.  
  6. inline void CopyAndNullTerminate( const std::string& source, char* dest, size_t dest_size )
  7. {
  8. source.copy( dest, dest_size );
  9. using std::min;
  10. char* end = dest + min( static_cast< size_t >( dest_size - 1 ), source.size() );
  11. *end = '\0';
  12. }
  13.  
  14. int main()
  15. {
  16. enum{ MAX_STRING_SIZE = 15 };
  17. char dest[ MAX_STRING_SIZE ];
  18. std::string test = "This is a test";
  19.  
  20. CopyAndNullTerminate( test, dest, MAX_STRING_SIZE );
  21.  
  22. assert( strcmp( test.c_str(), dest ) == 0 );
  23. return 0;
  24. }
Success #stdin #stdout 0.01s 2808KB
stdin
Standard input is empty
stdout
Standard output is empty