Reputation: 435
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;
}
Upvotes: 0
Views: 590
Reputation: 4370
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.
Upvotes: 5
Reputation: 213711
fopen_s
is only available in C11's optional bounds-checking library. In order to use it, you need to do:
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <errno.h>
... // rest of program
Then compile with -std=c11
and pray.
Because the bounds-checking library has poor compiler support and I'm not sure how much of it that gcc actually implemented. The general consensus among C programmers seems to be that the bounds-checking library is dangerous and should be avoided - it's release was a complete fiasco.
You are better off forgetting all about this library and use fopen
instead.
Upvotes: 2