简体   繁体   中英

My Qt program compiles but crashes when I use ifstream in declarations

I am currently trying to create a GUI on Qt for a program I wrote on visual studio. In vs there is no problem when I run the code.

I created a window that works well in Qt, and then I decided just to add a class from visual studio. I added them in the project, and compiled. There are no errors during the compilation. But then when I try to run the program, it crashes before being displayed with the following message : "The program has unexpectedly finished." even though I don't actually call any methods from that class.

It seems that if I take out all the parts of my code that uses the ifstream File, the it doesnt crash.

Here is the full cpp code from that class :

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

As I said if I take out the functions that use the ifstream, it runs correctly. And if I don't, it compiles, but crashes at execution.

Thanks a lot for your answers if you find any !

I Found out my problem, even if and don't see how it is related to ifstream. I needed to add the .dll files of openCV in the directory, I forgot to put them back. Therefore it compiled, but crashed at Startup. Hopes this helps ! And thanks for the help anyway :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM