`fopen`和QT(MinGW)出错

时间:2021-11-11 01:56:16

I have the following code in my QT application:

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

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

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

, where the type of m_fileName is std::string. This code works fine in Visual Studio2012. But in QT 4.7.4 (MinGW compiler) my program crashes at this line.

,其中m_fileName的类型是std :: string。此代码在Visual Studio2012中正常工作。但在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. I tried to print m_fileName in constructor and it crushed the program. Looks like m_fileName can't be initialized. But, I don't know why this happens.

是的,@ PaulMcKenzie是对的。我试图在构造函数中打印m_fileName,它破坏了程序。看起来m_fileName无法初始化。但是,我不知道为什么会这样。

Update 2

I figured out it crushes because of printf and other C i/o functions. Very strange.

我发现它因printf和其他C i / o功能而崩溃。很奇怪。

1 个解决方案

#1


Your code is syntax error. (bracket)

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

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

FILE * file = fopen(m_fileName.c_str()),“rb”); ^这一个

Did you compile successful?

你编译成功了吗?

#1


Your code is syntax error. (bracket)

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

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

FILE * file = fopen(m_fileName.c_str()),“rb”); ^这一个

Did you compile successful?

你编译成功了吗?