Segmentation Fault and Error in typedef struct

0

Hello, I'm developing a final work on a course and I'm getting two errors that I do not understand. First here's the code.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{

FILE *mestre, *indice;
int opcao, i=0;
char resp;

/* lay-out do arquivo mestre */
struct reg_mestre
{   int posicao;
    char nome_pacote[20];
    char destino[15];
    float preco;
    int nr_dias;
    char meio_transporte[15];
 }dados[50];

/* lay-out do arquivo de índices */
struct reg_indice
{   char nome_pacote[20];
    int posicao;
}dados2[50];


printf ("Bem vindo ao catalogo da agência de viagens!\n");
printf ("\nO que deseja fazer: \n");
printf ("\n1 - Adicionar\n");
printf ("2 - Remover\n");
printf ("3 - Alterar\n");
printf ("4 - Exibir todo o Catalogo\n");
printf ("5 - Consultar um destino específico\n");

printf("\n\nESCOLHA: ");
scanf ("%d", &opcao);

if (opcao == 1){

        mestre = fopen("//home//vitor//Desktop//mestre.bin", "ab");
        indice = fopen("//home//vitor//Desktop//indice.bin", "ab");

        if (((mestre = fopen("//home//vitor//Desktop//mestre.bin", "ab"))==NULL) || ((indice = fopen("//home//vitor//Desktop//indice.bin", "ab")==NULL))){ 
            printf("Erro na abertura do arquivo");              
        }

        else {
                do { 
                    i++;
                    printf ("--------------------------------------------");
                    printf ("\nPACOTE: %d\n",i); 
                    printf ("\nNome do Pacote: "); 
                    scanf ("%s",dados[i].nome_pacote);
                    fflush(stdin); 
                    printf ("Destino: ");               
                    scanf ("%s", dados[i].destino);
                    fflush(stdin); 
                    printf("Preço: ");
                    scanf ("%f", dados[i].preco);
                    fflush(stdin); 
                    printf ("Dias: ");
                    scanf ("%d", dados[i].nr_dias);
                    fflush(stdin); 
                    printf ("Meios de Transporte: ");
                    scanf ("%s", dados[i].meio_transporte);  
                    fflush(stdin); 
                    fprintf(mestre,"%d %s %s %f %d %s \n",i, dados[i].nome_pacote, dados[i].destino, dados[i].preco, dados[i].nr_dias, dados[i].meio_transporte);
                    fprintf(indice,"%d %s\n", i, dados2[i].nome_pacote); 
                    printf("Deseja digitar mais dados? (S=sim ou N=nao):"); 
                    fflush(stdin); 
                    scanf("%c",&resp); 
             } while (resp=='s' || resp == 'S'); 
             fclose(mestre); 
             fclose(indice);
             printf ("Deu Certo");
        } 

}


else if (opcao == 2 ){

        mestre = fopen("//home//vitor//Desktop//mestre.bin", "wb");
        indice = fopen("//home//vitor//Desktop//indice.bin", "wb");

        if (((mestre = fopen("//home//vitor//Desktop//mestre.bin", "ab"))==NULL) || ((indice = fopen("//home//vitor//Desktop//indice.bin", "ab")==NULL))){ 
            printf("Erro na abertura do arquivo");              
        }
        else {
                printf ("Deu certo");
            }       
    }


else if (opcao == 3 ){

        mestre = fopen("//home//vitor//Desktop//mestre.bin", "wb");
        indice = fopen("//home//vitor//Desktop//indice.bin", "wb");

        if (((mestre = fopen("//home//vitor//Desktop//mestre.bin", "ab"))==NULL) || ((indice = fopen("//home//vitor//Desktop//indice.bin", "ab")==NULL))){ 
            printf("Erro na abertura do arquivo");                  
        }

        else {
                printf("O arquivo abriu!");
        }       
}

else if (opcao == 4){

        mestre = fopen("//home//vitor//Desktop//mestre.bin", "rb");
        indice = fopen("//home//vitor//Desktop//indice.bin", "rb");

        if (((mestre = fopen("//home//vitor//Desktop//mestre.bin", "ab"))==NULL) || ((indice = fopen("//home//vitor//Desktop//indice.bin", "ab")==NULL))){ 
            printf("Erro na abertura do arquivo");              
        }

        else {
                printf("O arquivo abriu!");
        }       
    }

else if (opcao == 5){

        mestre = fopen("//home//vitor//Desktop//mestre.bin", "rb");
        indice = fopen("//home//vitor//Desktop//indice.bin", "rb");

        if (((mestre = fopen("//home//vitor//Desktop//mestre.bin", "ab"))==NULL) || ((indice = fopen("//home//vitor//Desktop//indice.bin", "ab")==NULL))){ 
            printf("Erro na abertura do arquivo");              
        }

        else {
                printf("O arquivo abriu!");
        }       
    }

return 0;

}

The first error occurs when I use typedef struct and then I try to generate a data vector so it looks something like this:

    typedef struct reg_mestre
{   int posicao;
    char nome_pacote[20];
    char destino[15];
    float preco;
    int nr_dias;
    char meio_transporte[15];
 };
 reg_mestre dados[50];

He says that it is not related to any union.

The second error happens when I'm running option 1, when I get in the price I get a segmentation fault, and even if I comment the line, and pass to number of days, it gives segmentation fault, any idea?

    
asked by anonymous 24.06.2014 / 17:56

1 answer

3

The syntax for structs is as follows:

struct nome {
    int membro1;
    int membro2;
    int membro3;
};

struct nome objeto;

Or:

typedef struct {
    int membro1;
    int membro2;
    int membro3;
} nome;

nome objeto;

Regarding segmentation fault errors, the solution is to run the code in a debugger. Use gdb to do this.

In your case, you have a scanf wrong. Note that the argument must be a pointer to wherever the read data is. This line:

scanf ("%f", dados[i].preco);

should be:

scanf ("%f", &dados[i].preco);

Note however that scanf ("%s", dados[i].destino); works because destino is an array.

    
24.06.2014 / 18:19