我开始为游乐园编写 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_s
是 fopen
的安全版本,因此如果您不想使用 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/