How can I use the fopen command?

0

When I try to use the fopen command Visual Studio returns an error saying that such a command is unsafe, and that I should use fopen_s, but in case I do not know which way to go with fopen_s.

    
asked by anonymous 04.05.2018 / 02:31

1 answer

0

According to the function documentation , the code snippet using fopen :

fp = fopen(filename, mode);

can be rewritten as

errno_t error_code = fopen_s(&fp, filename, mode);

where error_code equals 0 if the operation is successful or contains an error code if the operation fails. Note that the first argument of fopen_s is FILE** , that is, a pointer to a pointer. Therefore it is necessary to pass the address of fp to &fp .

With the exception of handling possible errors that should now consider the value of error_code , the rest of the code should work the same way it would work if the fopen function was used.

Issue:

I noticed that I did not answer your question directly. If you really want to use fopen (for portability reasons, for example), set the _CRT_SECURE_NO_DEPRECATE macro before including stdio.h :

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
    
04.05.2018 / 10:51