`fopen'和qt(mingw)的错误

Error with `fopen` and QT(MinGW)

本文关键字:mingw 错误 qt fopen      更新时间:2023-10-16

我在QT应用程序中具有以下代码:

FILE * file = fopen(m_fileName.c_str(), "rb");

,其中m_fileName的类型为std::string。此代码在Visual Studio2012中正常工作。但是在QT 4.7.4(mingw编译器)中,我的程序在此行崩溃了。

我真的不知道此代码怎么了。我没有经常使用mingw,所以一定有我不知道的东西。

更新:

main.cpp的代码:

std::string fileName = "test1.bmp";
m_pTexture1 = new Texture(GL_TEXTURE_2D, fileName);
if (!m_pTexture1->Load()) {
   return;
}

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

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

是的,@paulmckenzie是对的。我试图在构造函数中打印m_fileName,并粉碎了程序。看起来m_fileName无法初始化。但是,我不知道为什么会发生这种情况。

更新2

我发现它由于printf和其他C I/O功能而被压碎。非常奇怪。

您的代码是语法错误。(支架)

FILE * file = fopen(m_fileName.c_str()), "rb"); ^ this one

您是否成功?