0

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;

}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Rice Man
  • 415
  • 1
  • 5
  • 16

2 Answers2

5

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.

Community
  • 1
  • 1
Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29
  • Hmm yeah I heard some rumour about removing it in C2x too. Not sure if it has been confirmed yet. – Lundin Feb 20 '20 at 18:51
  • Not sure how much of a difference it will make removing it from C2x. I imagine most of the people who actually pay attention to the contents of the standard are already intentionally not using Annex K, and the others will still come across programs written for Windows that use them and assume they should be using them. – Christian Gibbons Feb 20 '20 at 18:54
  • 2
    Microsoft are using their confused non-standard version of it, while at the same time displaying confused warnings when using well-documented, perfectly safe standard functions. Maybe the worlds' C programmers can do a fundraiser to arrange *lots* of free booze for the MS people, every night before a C2x community meeting? :) – Lundin Feb 20 '20 at 18:58
  • 2
    Maybe we can get a sleeper-agent into Microsoft to get rid of those compiler warnings and add new warnings for their non-conforming functions telling users to use the regular functions instead. And also make their compiler C99 and C11 conformant while we're at it. Seriously, C99 has been around for over two decades now; it's time. – Christian Gibbons Feb 20 '20 at 19:02
  • @ChristianGibbons That was exactly my problem. I don't use c very often, so from my perspective I thought these were necessary functions. – Rice Man Feb 20 '20 at 19:17
2

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • You can check whether the functions are present — [`#ifdef __STDC_LIB_EXT1__`](http://port70.net/~nsz/c/c11/n1570.html#6.10.8.3) — and design your fallback for when that is not defined. – Jonathan Leffler Feb 20 '20 at 18:59
  • @Lundin, Thank you for the detail of adding #define __STDC_WANT_LIB_EXT__ 1 Earlier I had tried compiling with "gcc --std=c11 myProgram.c" But all it did was create a new warning where the word "warning" was in hot pink instead of white. – Rice Man Feb 20 '20 at 19:24