使用fopen而不是Visual Studio建议的fopen_s有什么错误?

时间:2017-04-01 15:05:11

标签: c++ opengl fopen

我开始研究Amusement Park的OpenGL代码。但我收到以下错误:

"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."

即使我尝试将其更改为fopen_s,也会出现更多错误。

这是代码部分:

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;
}

我在file = fopen声明中收到错误。

帮我查一下这个错误。

1 个答案:

答案 0 :(得分:1)

fopen_sfopen的安全版本,因此如果您不想使用fopen_s,请考虑使用_CRT_SECURE_NO_WARNINGS来消除该错误,因为您正在使用Visual Studio。

现在,如果您想使用fopen_s更正错误,则必须查看the documentation of fopen_s

使用示例:

errno_t returnValue = fopen_s(&file, fileName, "r");