Answer

问题及解答

关于 fopen() 函数

Posted by haifeng on 2021-03-02 13:44:00 last update 2021-03-02 15:55:32 | Edit | Answers (0)

在使用 VS2019 时, 如果代码中使用了 fopen() 函数, 则在编译时会显示如下警告.  C4996. 提示使用 fopen_s()

 

严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C4996 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. studentsMS D:\work\cs\c++\database\StudentsMangementSystem\studentsMS\studentsMS\studentsMS.cpp 28

 

fopen() 函数的原型是

FILE *fopen( const char *filename, const char *mode );

而 fopen_s() 函数的原型是

errno_t fopen_s( FILE** pFile, const char *filename, const char *mode );

 

fp = fopen("users.txt", "rb+");

应该为

fopen_s(&fp, "users.txt", "rb+");

 

 

 

References:

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-160

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-s-wfopen-s?view=msvc-160