I'm using Linux, compiling with gcc, getting error: warning: implicit declaration of function ‘fopen_s’, can someone help me fix this?

Rice Man :

I'm trying to write a simple edge detection program in c. I'm using Red Hat Enterprise Linux Server 7.7 (Maipo), and gcc version 4.8.5.

This is the start of the code:

#include <stdio.h>

#define size 200

int _tmain(int argc, _TCHAR* argv[])
{
    char filein[size] = "./image.bmp";

    FILE *fin;

    fopen_s(&fin, filein, "rb");

return 0;

}

I initially, had a lot of problems with _TCHAR* so eventually I replaced it with just char, I have no idea if this will be a problem later, but at least it compiled and got rid of those errors. Now I'm getting the implicit declaration warning. I've tried to fix it by adding other #include's.

I've tried to fix it with:

#include <stdio.h>
#include <errno.h>
#include <string.h>

#define size 200

int main(int argc, char* argv[])
{
    char filein[size] = "./image.bmp";

    FILE *fin;

    fopen_s(&fin, filein, "rb");

return 0;

}

But, I'm still getting the same warning, can someone tell me what I'm doing wrong?

Thanks.

Thank you so much, this works!

#include <stdio.h>

#define size 200

int main(int argc, char* argv[])
{

    char filein[size] = "./image.bmp";

    FILE *fin;

    fin = fopen(filein, "rb");

return 0;

}
Christian Gibbons :

the _s series of functions are optional functions from Annex K of the C standard, and rarely does any C implementation bother to implement Annex K. The actual utility of the "safe" functions introduced in Annex K is much disputed; just ditch those functions and use the standard functions such as fopen.

The only time I've ever come across the _s functions have been in code written for Windows, and Microsoft includes their own versions of these functions that do not conform to the standard set forth in Annex K.

See here for a study examining the utility of Annex K: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm

Their conclusion:

Despite more than a decade since the original proposal and nearly ten years since the ratification of ISO/IEC TR 24731-1:2007, and almost five years since the introduction of the Bounds checking interfaces into the C standard, no viable conforming implementations has emerged. The APIs continue to be controversial and requests for implementation continue to be rejected by implementers.

The design of the Bounds checking interfaces, though well-intentioned, suffers from far too many problems to correct. Using the APIs has been seen to lead to worse quality, less secure software than relying on established approaches or modern technologies. More effective and less intrusive approaches have become commonplace and are often preferred by users and security experts alike.

Therefore, we propose that Annex K be either removed from the next revision of the C standard, or deprecated and then removed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=12175&siteId=1
Recommended