jeudi 16 septembre 2021

.eof() doesn't recognize the end of the file after a .seekg(0, ios::beg) | C++

I'm using binary files with fstream, so, after reading the start of a new cycle, I use .eof() to end the function. Howevever, I need to read this file multiple times, so, everytime I call the function, I use .seekg(0, ios::beg) to "rewind" the file. The problem is, after I do this, .eof() stops working in that function, leading to a endless loop.

void iteracionTest(ifstream& binCond, ifstream& binFalt, ofstream& archReporte){
    int licencia;
    while(1){
        binCond.read(reinterpret_cast<char*>(&licencia), sizeof(int));
        if(binCond.eof())break;
        leerImprimirBinCond(binCond, archReporte, licencia);
        leerImprimirBinFalt(binFalt, archReporte, licencia);
    }
}

That one is just where I call the problem function.

void leerImprimirBinFalt(ifstream& binFalt, ofstream& archReporte, int licencia){
    char placa[20], gravedad[20];
    int binLicencia, dd, mm, yy, codigo;
    double multa;
    binFalt.seekg(0, ios::beg);
    while(1){
        binFalt.read(reinterpret_cast<char*>(placa), sizeof(char)*20);
        if(binFalt.eof())break;
        binFalt.read(reinterpret_cast<char*>(&binLicencia), sizeof(int));
        binFalt.read(reinterpret_cast<char*>(&dd), sizeof(int));
        binFalt.read(reinterpret_cast<char*>(&mm), sizeof(int));
        binFalt.read(reinterpret_cast<char*>(&yy), sizeof(int));
        binFalt.read(reinterpret_cast<char*>(gravedad), sizeof(char)*20);
        binFalt.read(reinterpret_cast<char*>(&codigo), sizeof(int));
        binFalt.read(reinterpret_cast<char*>(&multa), sizeof(double));
    }
}

When debugging, instead of stopping, "placa" continued to get filled with blank characters. And for some reason, it worked perfectly the first time the function is called, yet, it no longer does after that.

What kills me is that I used this same method for another program, and somehow, there it works perfectly.

    binAlu.seekg(0, ios::beg);
    while(1){
        binAlu.read(reinterpret_cast<char*>(&codigo), sizeof(int));
        if(binAlu.eof())break;
        binAlu.read(reinterpret_cast<char*>(name), sizeof(char)*50);
        binAlu.read(reinterpret_cast<char*>(&tipo), sizeof(char));
        binAlu.read(reinterpret_cast<char*>(&carne), sizeof(int));
        binAlu.read(reinterpret_cast<char*>(facultad), sizeof(char)*10);
        binAlu.read(reinterpret_cast<char*>(especialidad), sizeof(char)*30);
    }

In order to make it work, I had to instead calculate the number of registers in the file and use for, to manually count to the end of the file.

void leerImprimirBinFalt(ifstream& binFalt, ofstream& archReporte, int licencia){
    char placa[20], gravedad[20];
    int binLicencia, dd, mm, yy, codigo, tamano=sizeof(char)*40+sizeof(int)*5+sizeof(double), tamanoA, cant, i;
    double multa;
    binFalt.seekg(0, ios::end);
    tamanoA=binFalt.tellg();
    binFalt.seekg(0, ios::beg);
    cant=tamanoA/tamano;
    for(i=0; i<cant; i++){
        binFalt.read(reinterpret_cast<char*>(placa), sizeof(char)*20);
        binFalt.read(reinterpret_cast<char*>(&binLicencia), sizeof(int));
        binFalt.read(reinterpret_cast<char*>(&dd), sizeof(int));
        binFalt.read(reinterpret_cast<char*>(&mm), sizeof(int));
        binFalt.read(reinterpret_cast<char*>(&yy), sizeof(int));
        binFalt.read(reinterpret_cast<char*>(gravedad), sizeof(char)*20);
        binFalt.read(reinterpret_cast<char*>(&codigo), sizeof(int));
        binFalt.read(reinterpret_cast<char*>(&multa), sizeof(double));
    }
}

Am I missing something?

Aucun commentaire:

Enregistrer un commentaire