当我在声明中使用ifstream时,我的Qt程序编译但崩溃

时间:2017-04-12 16:14:50

标签: c++ qt crash ifstream

我目前正在尝试在Qt上为我在visual studio上编写的程序创建一个GUI。在vs中,运行代码时没有问题。

我创建了一个在Qt中运行良好的窗口,然后我决定只从visual studio中添加一个类。我在项目中添加了它们并进行了编译。编译期间没有错误。但是当我尝试运行程序时,它会在显示之前崩溃,并显示以下消息:"程序意外完成。"即使我实际上并没有从该课程中调用任何方法。

似乎如果我取出使用ifstream文件的代码的所有部分,它就不会崩溃。

以下是该类的完整cpp代码:

#include "tableManager.h"

ifstream ifsFile;    // Stream du fichier en lecture

tableManager::tableManager(void)
{
    setTypeS();
}

tableManager::tableManager(string fileN)
{
    setTypeS();
    openFile(fileN);
}


tableManager::~tableManager(void)
{
    ifsFile.close();
}
string tableManager::getFileName()
{
    return fileName;
}

int tableManager::getNbrTables()
{
    return nbrTables;
}

int tableManager::getTypeData(int i)
{
    return typeData[i];
}

string tableManager::getTypeDataS(int i)
{
    return typeDataS[typeData[i]];
}

string tableManager::getName(int i)
{
    return name[i];
}

int tableManager::getNbrL(int i)
{
    return nbrL[i];
}

int tableManager::getNbrC(int i)
{
    return nbrC[i];
}

int tableManager::openFile(string fileN)
{

    fileName = fileN;
    ifsFile.open(fileName, ios::in | ios::binary);
    if(ifsFile.is_open())
    {
        std::cout << "Lecture du fichier binaire" << endl << endl;
        nbrTables = readNbrTables();
        for(int i = 0; i < nbrTables; i++)
        {
            std::cout << endl;
            typeData[i] = readTypeData(i);
            name[i] = readTableName(i);
            labelP.push_back(name[i]);
            nbrL[i] = readNbrL(i);
            nbrC[i] = readNbrC(i);

            if(!readDataOCV(i))
            {
                std::cout << "Les donnees n'ont pu etre chargees" << endl;
                return 0;
                break;
            }
        }
    }
    else
    {
        std::cout << "Erreur de lecture du fichier binaire" << endl;
        return 0;
    }
    std::cout << endl;
    return 1;
}

int tableManager::setTypeS()
{
    typeDataS[0] = "char";
    typeDataS[1] = "unsigned char";
    typeDataS[2] = "short";
    typeDataS[3] = "unsigned short";
    typeDataS[4] = "integer";
    typeDataS[5] = "unsigned integer";
    typeDataS[6] = "float";
    typeDataS[7] = "double";
    typeDataS[8] = "MAC adress";
    return 1;
}

uint32_t tableManager::readNbrTables()
{
    uint32_t a;
    ifsFile.read(reinterpret_cast<char *>(&a), sizeof(a));
    std::cout << "Nbr de tables : " << a << endl;
    return a;
}

uint32_t tableManager::readTypeData(int k)
{
    uint32_t a;
    ifsFile.read(reinterpret_cast<char *>(&a), sizeof(a));
    std::cout << "Type de donnees de la " << (k+1) << "e table : " << a - 1 << " --> " << typeDataS[a - 1] << endl;
    return (a - 1);
}

string tableManager::readTableName(int i)
{
    uint32_t a;
    ifsFile.read(reinterpret_cast<char *>(&a), sizeof(a));
    nameLength[i] = a;
    string nameTmp;
    char c;
    for(int l = 0; l < a; l ++)
    {
        ifsFile.read(&c, sizeof(c));
        nameTmp += c;
    }
    std::cout << "Le nom de la " << (i + 1) << "e table : " << nameTmp << endl;
    return nameTmp;
}

uint32_t tableManager::readNbrL(int j)
{
    uint32_t a;
    ifsFile.read(reinterpret_cast<char *>(&a), sizeof(a));
    std::cout << "Nbr de lignes de la " << (j+1) << "e table : " << a << endl;
    return a;
}

uint32_t tableManager::readNbrC(int l)
{
    uint32_t a;
    ifsFile.read(reinterpret_cast<char *>(&a), sizeof(a));
    std::cout << "Nbr de colonnes de la " << (l+1) << "e table : " << a << endl;
    return a;
}


vector<Mat> tableManager::getDataP()
{
    return dataP;
}
vector<string> tableManager::getLabelP()
{
    return labelP;
}

int tableManager::readDataOCV(int m)
{
    int cpt = 0;
    int total = nbrC[m]*nbrL[m];
    if(typeData[m]==8) nbrL[m]=6;
    Mat tmpM(Size(nbrC[m], nbrL[m]), CV_64FC1);

    switch (typeData[m])
    {
    case 0:
        char x0;
        tmpM.empty();
        for(int n = 0; n < nbrC[m]; n++)
        {
            for(int o = 0; o < nbrL[m]; o++)
            {
                ifsFile.read(reinterpret_cast<char *>(&x0), sizeof(x0));
                tmpM.at<double>(Point(n,o)) = x0;
                cpt++;
            }
        }
        dataP.push_back(tmpM);
        std::cout << "Les " << cpt << "/" << total << " donnees ont ete chargees avec succes" << endl;
        break;
    case 1:
        unsigned char x1;
        tmpM.empty();
        for(int n = 0; n < nbrC[m]; n++)
        {
            for(int o = 0; o < nbrL[m]; o++)
            {
                ifsFile.read(reinterpret_cast<char *>(&x1), sizeof(x1));
                tmpM.at<double>(Point(n,o)) = x1;
                cpt++;
            }
        }
        dataP.push_back(tmpM);
        std::cout << "Les " << cpt << "/" << total << " donnees ont ete chargees avec succes" << endl;
        break;
    case 2:
        short x2;
        tmpM.empty();
        for(int n = 0; n < nbrC[m]; n++)
        {
            for(int o = 0; o < nbrL[m]; o++)
            {
                ifsFile.read(reinterpret_cast<char *>(&x2), sizeof(x2));
                tmpM.at<double>(Point(n,o)) = x2;
                cpt++;
            }
        }
        dataP.push_back(tmpM);
        std::cout << "Les " << cpt << "/" << total << " donnees ont ete chargees avec succes" << endl;
        break;
    case 3:
        unsigned short x3;
        tmpM.empty();
        for(int n = 0; n < nbrC[m]; n++)
        {
            for(int o = 0; o < nbrL[m]; o++)
            {
                ifsFile.read(reinterpret_cast<char *>(&x3), sizeof(x3));
                tmpM.at<double>(Point(n,o)) = x3;
                cpt++;
            }
        }
        dataP.push_back(tmpM);
        std::cout << "Les " << cpt << "/" << total << " donnees ont ete chargees avec succes" << endl;
        break;
    case 4:
        int x4;
        tmpM.empty();
        for(int n = 0; n < nbrC[m]; n++)
        {
            for(int o = 0; o < nbrL[m]; o++)
            {
                ifsFile.read(reinterpret_cast<char *>(&x4), sizeof(x4));
                tmpM.at<double>(Point(n,o)) = x4;
                cpt++;
            }
        }
        dataP.push_back(tmpM);
        std::cout << "Les " << cpt << "/" << total << " donnees ont ete chargees avec succes" << endl;
        break;
    case 5:
        unsigned int x5;
        tmpM.empty();
        for(int n = 0; n < nbrC[m]; n++)
        {
            for(int o = 0; o < nbrL[m]; o++)
            {
                ifsFile.read(reinterpret_cast<char *>(&x5), sizeof(x5));
                tmpM.at<double>(Point(n,o)) = x5;
                cpt++;
            }
        }
        dataP.push_back(tmpM);
        std::cout << "Les " << cpt << "/" << total << " donnees ont ete chargees avec succes" << endl;
        break;
    case 6:
        float x6;
        tmpM.empty();
        for(int n = 0; n < nbrC[m]; n++)
        {
            for(int o = 0; o < nbrL[m]; o++)
            {
                //cout << "test 1" <<endl;
                ifsFile.read(reinterpret_cast<char *>(&x6), sizeof(x6));
                //cout << "test 2" << endl;
                tmpM.at<double>(Point(n,o)) = x6;
                cpt++;
                //cout << "test 3" << endl;
            }
        }
        dataP.push_back(tmpM);
        std::cout << "Les " << cpt << "/" << total << " donnees ont ete chargees avec succes" << endl;
        break;
    case 7:
        double x7;
        tmpM.empty();
        for(int n = 0; n < nbrC[m]; n++)
        {
            for(int o = 0; o < nbrL[m]; o++)
            {
                ifsFile.read(reinterpret_cast<char *>(&x7), sizeof(x7));
                tmpM.at<double>(Point(n,o)) = x7;
                cpt++;
            }
        }
        dataP.push_back(tmpM);
        std::cout << "Les " << cpt << "/" << total << " donnees ont ete chargees avec succes" << endl;
        break;
    case 8:
        uint8_t x8;
        tmpM.empty();
        for(int n = 0; n < nbrC[m]; n++)
        {
            for(int o = 0; o < nbrL[m]; o++)
            {
                ifsFile.read(reinterpret_cast<char *>(&x8), sizeof(x8));
                tmpM.at<double>(Point(n,o)) = x8;
            }
        }
        dataP.push_back(tmpM);
        std::cout << "Les " << cpt << "/" << total << " donnees ont ete chargees avec succes" << endl;
        break;
    default:
        std::cout << "Erreur de lecture des donn�es, type non d�fini";
        return 0;
        break;
    }
    return 1;
}

正如我所说,如果我取出使用ifstream的函数,它会正确运行。如果我不这样做,它会编译,但会在执行时崩溃。

如果你找到任何答案,非常感谢你的答案!

1 个答案:

答案 0 :(得分:0)

我发现了我的问题,即使并且没有看到它与ifstream的关系。我需要在目录中添加openCV的.dll文件,我忘了把它们放回去。因此它编译了,但在Startup崩溃了。希望这有帮助! 无论如何,谢谢你的帮助:)。