Home > Mobile >  problem in using fprintf_s(getting warnings) in c
problem in using fprintf_s(getting warnings) in c

Time:04-30

I'm new in c.I just want to complete my project.a list of student with struct.and one of the option is save data in a file. I using visual studio 2022 and BTW I can't use syntaxs like scanf or something like that.I have to use scanf_s and etc anyway for fprintf_s I don't know how can I use it for my project.I searched in stackoverflow and another sites about it syntax but they were not usefull, but vs gives me a warning. it is:

sttp could be '0'

the source code is :

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

and there are some warnings the image of error list

CodePudding user response:

When you try to open a file with fopen_s function, it may or may not succeed, so you must check the return value of that invocation.

If for some reason, the file was not opened, then your file pointer variable, sttp will not be initialized, hence the VS IDE is showing you the warning.

Modify your code, like below.

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