Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have this code that reads a file that already exists, but in the first "for" when it read the file and copies it to the "struct" when the index is 1, I get an error:

(A buffer Read overrun has occurred in File.exe Which has corrupted the program's internal state. Press Break to debug the program or Continue to terminate the program.

For more details please see Help topic 'How to debug Buffer Overrun Issues'.)

#include "lerArquivo.h"

void main()
{
    int nQtdRegistro;
    int i;

    REGISTRO stRegistro;
    REGISTRO *ptrSalva;
    REGISTRO *ptrVetRegistro;
    LABEL stLabel;
    FILE *fdArq;

    fdArq = fopen(CAMINHO_NOME_ARQ, "rb");
    if(fdArq == NULL)
    {
        cout << "Erro na abertura do arquivo: " << CAMINHO_NOME_ARQ<< endl;     
    }//if
    if(fread(&stLabel, sizeof(stLabel), 1 , fdArq) == 0)
    {
        fclose(fdArq);      // fechar o arquivo
        cout << "Erro fread" << endl;
        PAUSA;
        exit(-1);;  
    }

    ptrSalva = ptrVetRegistro = (REGISTRO *) malloc(stLabel.nQtdeRegistros * sizeof(REGISTRO));
    if(ptrSalva == NULL)
    {
        cout<< "Não tem memoria para conter " << stLabel.nQtdeRegistros << " registros" << endl;
        PAUSA;
        exit(-1);
    }

    for(i = 0; i < stLabel.nQtdeRegistros; i++)
        {
            if(fread(&stRegistro + i, sizeof(stRegistro), 1 , fdArq) == 0)
            {
                fclose(fdArq);      // fechar o arquivo
                cout << "Erro fread" << endl;
                PAUSA;
                exit(-1);   
            }       
            memcpy(ptrVetRegistro + (i * sizeof(REGISTRO)) , &stRegistro, sizeof(REGISTRO));
        }// for

    for(i = 0; i < stLabel.nQtdeRegistros; i++)
    {
        cout << "Descrição: " << ptrVetRegistro[i].cDescricao << "\n";
        cout << "Preço Unitario: " << ptrVetRegistro[i].dPrecoUnitario << "\n";
        cout << "Quantidade: " << ptrVetRegistro[i].dQuatindade << "\n";
    }
    fclose(fdArq);
    free(ptrSalva);
}
share|improve this question
The problem is here if(fread(&stRegistro + i, sizeof(stRegistro), 1 , fdArq) == 0). The variable stRegistro is not an array but a single value (so you should not be adding i). Maybe this should be ptrSalva. – Loki Astari Nov 1 '12 at 15:24
PS. This is C not C++ code. – Loki Astari Nov 1 '12 at 15:24
PPS. This is the wrong site for this type of question. If you need to fix a bug ask on stackoverflow.com. There are more people over there and thus you are likely to get a quicker better response than over here. – Loki Astari Nov 1 '12 at 15:26
PPPS. This is also wrong ptrVetRegistro + (i * sizeof(REGISTRO)) You need to look up pointer airthmatic. – Loki Astari Nov 1 '12 at 15:28

closed as off topic by Loki Astari, codesparkle, Jeff Vanzella, palacsint, Glenn Rogers Nov 1 '12 at 17:34

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

up vote 1 down vote accepted

The same code written in C++ would look like this:

int main()
{
    try
    {
        std::ifstream file("CAMINHO_NOME_ARQ", std::ios::binary);

        LABEL stLabel;
        file >> stLabel;

        std::vector<REGISTRO> salva;
        std::copy(std::istream_iterator<REGISTRO>, std::istream_iterator<REGISTRO>(),
                  std::back_inserter(salvo)
                 );

        std::copy(salvo.begin(), salvo.end(),
                  std::ostream_iterator<REGISTRO_Pretty>(std::cout)
                 );
    }
    catch(std::exception const& e)
    {
        std::cerr << "Exception: " << e.what() << "\n";
    }
}
share|improve this answer
in stackoverflow.com the said that it was the wrong site to, but thanks anyway, now it's clear and i see that i have no ideia how to code in C++. – Yuri Freire Nov 1 '12 at 15:58
Missing a return. – asveikau Nov 1 '12 at 17:36
@YuriFreire: It fit's better on SO because it has a bug. Our FAQ (point 4 off topic) explicitly says that we don't look at code with bugs – Loki Astari Nov 1 '12 at 17:49
@asveikau: No its not. In C++ if main() does not have a return then the compiler will plant a return 0;. This is usually done to indicate that the program does not indicate failure. – Loki Astari Nov 1 '12 at 17:50
@Loki - Ah, right, I knew that at one point, but I forgot. (It's kind of stupid that this made it into the language. I would prefer to be explicit, and not have something that "looks like a bug" in this way be correct.) – asveikau Nov 1 '12 at 18:05
show 1 more comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.