C program working but not continuing, gives error at the end

Asked

Viewed 42 times

-2

This is a program that I’ve been doing for presentation only that part of it is making mistakes that I haven’t been able to solve.
Here is the . h:

#ifndef PROJETOIAPG_PROJETO_H
#define PROJETOIAPG_PROJETO_H
#define TAM_S 30
typedef struct cliente {
char nome[15], sobrenome[15];
int id;
} CLIENTE;

int inicializacao();

int ler_clientes(CLIENTE cliente[]);
#endif

And here’s the . c

#include stdlib.h
#include projeto.h
#include stdio.h

int inicializacao(){              
    CLIENTE cliente;
    ler_clientes(&cliente);
    menu_principal();
    return 0;
}

int ler_clientes(CLIENTE cliente[]) {
    int i=0,num_c=0;
    FILE fp;
    fp = fopen(Clientes.txt,r);

    if( fp == NULL ) {
        printf(nErro!n);
    }
    else
    {
        fscanf(fp , %dn , &num_c);
        for(i=1;inum_c+1;i++)
        {
            fscanf(fp , %d  , &cliente[i].id);
            fscanf(fp , %s  , &cliente[i].nome);
            fscanf(fp , %sn , &cliente[i].sobrenome);

            printf(nid%d  , cliente[i].id);
            printf(nnome%s  , cliente[i].nome);
            printf(nsobrenome%s , cliente[i].sobrenome);
        }
    }
    fclose(fp);
    return 0;
}

The.txt Clients file looks like this:

5
1 Manuel Monteiro
2 Maria Fernandes
3 Ines Cornio
4 Fernanda Guimaraes
5 Fernanda Guimaraes

The problem is that I haven’t been able to solve the error yet, since the most I’ve done so far is to reduce the . h the size of the character name[30] for 15 and now it is possible to run to the end but still gives the error:
Process finished with Exit code -1073740791 (0xC0000409)

  • Welcome to Stack Overflow in English. Please click edit and translate the question.

  • Sorry, I didn’t know the site existed in en. It is translated.

  • The parameter referring to the format of your fscanf and printf functions is a string and therefore must be in quotes. As you say the program is working then I think there was a transcription error because, in this way, it would not compile.

1 answer

1


Just look, this program has numerous errors, clearly you need to study the language from the beginning, before trying to make programs. Verbi gratia:

1) Its function ler_clientes() expects a matrix, ie a collection of structures CLIENTE, but you pass the address of a single structure (customer). When trying to write data from the second element on, you are already overwriting other things in memory, which end up breaking your program. And it’s better to break than to proceed with the corrupted memory...

Probably declare CLIENTE cliente[5]; resolves, although it would be more correct to use the name "clients".

2) for(i=1; inum_c+1; i++) usually C matrices begin by addressing by element 0, so you would have to start with i=0. Even if your matrix was correct with 5 elements, the code will override the element cliente[5] which is actually the sixth element.

inum_c+1 also makes no sense as final test of loop. There must be some problem in gluing the code, as the number of loops is given by the file size, would somehow pass the length of the array of clients as additional parameter of ler_clientes() and test whether i < tamanho_da_matriz.

The size of structure members (first and last name) is not causing problems.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.