How to copy a string into a char array in C++ without going over the buffer

前端 未结 11 976
Happy的楠姐
Happy的楠姐 2020-12-08 03:13

I want to copy a string into a char array, and not overrun the buffer.

So if I have a char array of size 5, then I want to copy a maximum of 5 bytes from a string in

11条回答
  •  误落风尘
    2020-12-08 03:43

    Some nice libc versions provide non-standard but great replacement for strcpy(3)/strncpy(3) - strlcpy(3).

    If yours doesn't, the source code is freely available here from the OpenBSD repository.

提交回复
热议问题