1

I started working on the OpenGL code for Amusement Park. but I'm getting the following error:

"error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details."

even if I try to change it frm fopen to fopen_s, there more errors.

This is the code part:

GLuint LoadBMP(const char *fileName)
{
    FILE *file;
    unsigned char header[54], *data;
    unsigned int dataPos, size, width, height;
    file = fopen(fileName, "rb");
    fread(header, 1, 54, file);             //Windows BMP begin with 54 byte header
    dataPos = *(int*)&(header[0x0A]);   //dec10, Actual BMP data
    size = *(int*)&(header[0x22]);  //dec34, BMP Size
    width = *(int*)&(header[0x12]); //dec18, Image Width
    height = *(int*)&(header[0x16]);    //dec22, Image Height
    if (size == NULL)
        size = width * height * 3;
    if (dataPos == NULL)
        dataPos = 54;
    data = new unsigned char[size];
    fread(data, 1, size, file);
    fclose(file);
    GLuint texture;
    glGenTextures(1, &texture);             //Generate (allocate) 1 texture name
    glBindTexture(GL_TEXTURE_2D, texture);  //Bind the 2D texture

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  //MAG filter
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  //MIN filter

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, data); //target, level, internalFormat, width, height,border, format, type, data 
    return texture;
}

I'm getting error in the file = fopen statement.

Help me to find the error in this.

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
AmrutaMH
  • 11
  • 1
  • 2
  • 3
    There's nothing wrong with that line of code. Apparently you're using some compiler setting that turns warnings into errors, which in turn lets Microsoft's paranoia control how you write code. – Pete Becker Apr 01 '17 at 15:19
  • 2 choices: (1) define the macro as show in the error message; or (2) show us the error you get with `fopen_s` (note it takes different parameters and returns an error code). – Richard Critten Apr 01 '17 at 15:19
  • @PeteBecker `fopen_s` is C11 – Richard Critten Apr 01 '17 at 15:20
  • 2
    @RichardCritten -- the warning/error is Microsoft. – Pete Becker Apr 01 '17 at 15:22
  • 1
    Before starting to use the `*_s` functions in C++ you might want to consider [C document N1967](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm) proposing its removal from the next C standard. – Bo Persson Apr 01 '17 at 19:39
  • *`fopen_s` is C11* No, `fopen_s` is from the **optional** Annex K of the C11 standard. Effectively that has only been implemented by Microsoft - but their implementation doesn't comply with the standards set in Annex K. So using Microsoft's "safer" functions is doubly non-portable: no one else implemented them, and Microsoft didn't meet the standard anyway. – Andrew Henle Oct 23 '19 at 16:00

1 Answers1

1

fopen_s is the secure version of fopen so if you don't want to use fopen_s consider using _CRT_SECURE_NO_WARNINGS to get rid of that error, since you are using Visual Studio.

Now if you want to use fopen_s to correct the error, you have to look at the documentation of fopen_s

Example of use:

errno_t returnValue = fopen_s(&file, fileName, "r");
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
  • It's probably more accurate to say `fopen_s()` is a part of Microsoft's attempt to [embrace, extend, and extinguish](https://en.wikipedia.org/wiki/Embrace,_extend,_and_extinguish) portable C code (those are Microsoft's own terms for how to take over a market...). Using `fopen_s()` does not make your code more secure, but it does make your code less portable. See [**N1967 - Field Experience With Annex K — Bounds Checking Interfaces**](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm) – Andrew Henle Mar 22 '21 at 18:35