VS2019中经常会遇到fopen打开文件错误,此时需要改为fopen_s
fopen用法:FILE* fp;
fp=fopen(filename,“w”);
fopen_s用法:errno_t err;
err=fopen_s(&fp,filename,“w”);
如:
errno_t err = fopen_s(&fp, "D:\Stu\stuSys","w");
if (err)
{
printf("文件打开错误,程序无法进行\n");
exit(0);
}
两者区别:
1.fopen打开文件成功,则返回文件指针给fp,打开文件失败则返回NULL。fopen_s打开成功返回0,打开失败返回1。
2.fopen_s比fopen多了溢出检测,所以在vs中使用fopen时会显示警告:warning 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.