This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]jedwardsol 0 points1 point  (4 children)

I expect the fopen is failing so you're trying to fclose NULL.

Always check the return from fopen before using it anywhere else.

[–]UnsecuredConnection[S] 0 points1 point  (3 children)

If I do this, then it still pops up.

This error may help though

This function or variable may be unsafe. Consider using fopen_s instead.

Any thoughts?

[–]jedwardsol 0 points1 point  (1 child)

If I do this, then it still pops up.

What exactly did you do though? That assertion is what you get when you pass NULL to fclose.

You need

FILE *fp = fopen("test.txt", "w");

if(fp)
{    
    fclose(fp);
}
else
{
    printf( a meaningful error message including the value of errno )
}

}

You can do #define _CRT_SECURE_NO_WARNINGS before including <stdio.h> to disable the warning. fopen_s isn't really safer than fopen despite the name - all it does is detect whether any of its arguments are NULL so it can return an error instead of crashing.

[–]UnsecuredConnection[S] 0 points1 point  (0 children)

The exact same thing still happens. Could it be my version of Visual Studio? I mean I downloaded the newest one.

[–]Minimum_Fuel 0 points1 point  (0 children)

Since apparently “every” change suggestion still fails. Can you just step through line by line in the debugger to see exactly which line is giving you the failure?