fopen_s
is a variant of fopen
which contains parameter validation and hands back an error code instead of a pointer in case something goes wrong during the open process. It's more secure than the base variant because it accounts for more edge conditions. The compiler is warning you to use it because fopen
represents a potential exploitation vector in your application.
You can specify digits of precision to the printf
family of functions by using the specifier %.xg
, where x is the digits of precision you want in the output. A long double
varies in precision from platform to platform, but you can generally bet on it being at least 16 digits of decimal precision.
Edit: While I'm not entirely on board with the others who are suggesting that fopen_s
is a complete waste of time, it does represent a pretty low chance of exploitation and it isn't widely supported. Some of the other functions warned about under C4996 are much more serious vulnerabilities, however, and using _CRT_SECURE_NO_WARNINGS
is the equivalent of turning off the alarm for both "you left your bedroom door unlocked" and "you left a nuclear bomb in the kitchen".
As long as you aren't restricted to using "pure C" for your project (e.g. for a school assignment or an embedded microcontroller), you would do well to exploit the fact that almost all modern C compilers are also C++ compilers and use the C++ iostream
variants of all of these I/O functions in order to get both improved security and compatibility at the same time.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…