How do I use the fopen command?

Asked

Viewed 322 times

0

Whenever I try to use the fopen command Visual Studio returns me an error saying that such command is insecure, and that I should use fopen_s, but in case I do not know what direction to take with fopen_s.

  • Since you are programming in C++ it would not be better to use a stream?

  • But how would I fit a stream into a Keylogger?

  • Also I could never use the VS keeps filling the patience with this

  • Yes, but I understand the VS, fopen can be manipulated, so the fopen_s, but the structures of the code change and I do not know how to proceed

  • 1

    Just as a hint, use fopen_s is decommissioned for portability reasons. The best solution would be just what @Maniero said and follow up with a stream solution that is normal for C++. There’s another hint "don’t complicate something simple".

1 answer

0


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

fp = fopen(filename, mode);

can be rewritten as

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

in which error_code is equal to 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**, i.e., a pointer to a pointer. It is therefore necessary to pass the address of fp as &fp.

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

Editing:

I realized I didn’t answer your question directly. If you really want to use fopen (for portability reasons, for example), set the macro _CRT_SECURE_NO_DEPRECATE before including stdio.h:

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
  • Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.