What are some alternatives to the Microsoft security enhanced functions such as strncpy_s
or _itoa_s
? Although developing in MS environment the goal is to write code that could be ported easily to other platforms.
If you really want to program in C:
Use the plain old standard strncpy
.
If you're programming in C++:
Use the plain old standard string class std::string
.
(hint: You probably want the latter. C strings are just bugs waiting to happen, even if you use the "secure" *_s
functions. C++ added a string class for a reason)
std::string s(Cstring, n);
Try strlcpy. Though not standard C, it exists on a number of platforms and you can download implementations as the source code is available free of charge. See http://www.gratisoft.us/todd/papers/strlcpy.html
Rageous is correct, there is no complex logic behind it.
I would just use Microsoft's version for now and if you decide to port to another OS later, THEN implement it yourself for the target platform and use preprocessor commands to specify your implementation on the non-Windows platform(s).
Just use strncpy and disable the warning in VC++.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With