[英]Error with `fopen` and QT(MinGW)
I have the following code in my QT application: 我的QT应用程序中包含以下代码:
FILE * file = fopen(m_fileName.c_str(), "rb");
, where the type of m_fileName
is std::string
. ,其中
m_fileName
的类型为std::string
。 This code works fine in Visual Studio2012. 此代码在Visual Studio2012中可以正常工作。 But in QT 4.7.4 (MinGW compiler) my program crashes at this line.
但是在QT 4.7.4(MinGW编译器)中,我的程序在这一行崩溃了。
I don't really know what is wrong with this code. 我真的不知道这段代码有什么问题。 I haven't used MinGW a lot, so there must be something I don't know about.
我没用过MinGW,所以肯定有些我不了解的东西。
Update: 更新:
Code from main.cpp
: 来自
main.cpp
代码:
std::string fileName = "test1.bmp";
m_pTexture1 = new Texture(GL_TEXTURE_2D, fileName);
if (!m_pTexture1->Load()) {
return;
}
Simplified code of Texture.cpp
: Texture.cpp
简化代码:
Texture::Texture(GLenum TextureTarget, std::string FileName)
{
m_textureTarget = TextureTarget;
m_fileName = FileName;
}
bool Texture::Load()
{
try {
FILE * file = fopen(m_fileName.c_str(),"rb");
}
catch (...) {
std::cout « "Error loading texture '" « m_fileName;
return false;
}
}
Code of Texture.h
: Texture.h
代码:
class Texture
{
public:
Texture(GLenum TextureTarget, std::string FileName);
bool Load();
private:
std::string m_fileName;
GLenum m_textureTarget;
GLuint m_textureObj;
unsigned int width, height;
unsigned char * data;
};
Yes, @PaulMcKenzie was right. 是的,@ PaulMcKenzie是正确的。 I tried to print
m_fileName
in constructor and it crushed the program. 我试图在构造函数中打印
m_fileName
,它粉碎了程序。 Looks like m_fileName
can't be initialized. 看起来
m_fileName
无法初始化。 But, I don't know why this happens. 但是,我不知道为什么会这样。
Update 2 更新2
I figured out it crushes because of printf
and other C i/o functions. 我发现它由于
printf
和其他C I / O功能而崩溃。 Very strange. 很奇怪。
Your code is syntax error. 您的代码是语法错误。 (bracket)
(托架)
FILE * file = fopen(m_fileName.c_str()), "rb"); ^ this one
Did you compile successful? 你编译成功了吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.