c++ - 按照 Visual Studio 的建议,使用 fopen 而不是 fopen_s 有什么错误?

标签 c++ opengl fopen

我开始为游乐园编写 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."

即使我尝试将其从 frm fopen 更改为 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 语句中遇到错误。

帮我找出这里的错误。

最佳答案

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");

关于c++ - 按照 Visual Studio 的建议,使用 fopen 而不是 fopen_s 有什么错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43158426/

相关文章:

c++ - 异常情况下的智能指针析构函数

c++ - 从 OpenGL 应用程序中提取颜色/深度缓冲区

java - NiftyGUI的Nifty类无法实例化?

opengl - gluPerspective、glViewport、gluLookAt 以及 GL_PROJECTION 和 GL_MODELVIEW 矩阵

c++ - 两个图像之间的像素坐标

c++ - 无法将 1 和 0 的字符串写入二进制文件,C++

c - 在纯 C 中打开一个 Unicode 文件

c - 文件描述符消失/无效

c++ - 使用打印不同范围的 3 个线程按排序顺序打印数字

c++ - 我应该如何考虑 fread() 的参数?