在 c 中使用 fprintf_s(收到警告)的问题

problem in using fprintf_s(getting warnings) in c

我是 c.I 的新人,只想用 struct.and 完成我的 project.a 学生列表,其中一个选项是将数据保存在文件中。 我使用 visual studio 2022 顺便说一句,我不能使用 scanf 之类的语法或 that.I 之类的语法,必须使用 scanf_s 等 无论如何 fprintf_s 我不知道如何将它用于我在 Whosebug 和其他网站上搜索的 project.I 语法,但它们没有用,但 vs 给了我一个警告。它是:

sttp could be '0'

源代码是:

FILE* sttp;
fopen_s(&sttp, "text.txt", "w");
fprintf_s(sttp, "test");
fclose(sttp);

还有一些警告the image of error list

当您尝试使用 fopen_s 函数打开文件时,它可能 成功,因此您必须检查 return 的值调用。

如果由于某种原因,文件没有打开,那么你的文件指针变量,sttp 将不会被初始化,因此 VS IDE 会向你显示警告。

修改您的代码,如下所示。

FILE* sttp;
if(0 == fopen_s(&sttp, "text.txt", "w") )
{
   fprintf_s(sttp, "test");
   fclose(sttp);
}