Segmentation Fault and Error in typedef struct

Asked

Viewed 156 times

0

Hello, I’m developing a final work of a discipline and I’m getting two mistakes I don’t 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 try to generate a data vector so it looks 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 it’s not related to any union.

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

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 on a Debugger. Use gdb to do so.

In your case, you have a scanf wrong. Note that the argument must be a pointer to where you want to put the data read. That 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.

  • Thank you very much, I didn’t know I should use the address for the second case.

  • however I keep getting Segmentation fault, only now on the means of transport.

  • Adjusted nr_dias?

  • yes, I will update the code above with the changes.

  • updated code.

  • I updated once again because I had forgotten in the first fprintf a %s of the transport, but in the program here is with it and the error course continues.

  • Do not edit the bridge question to make the answer become outdated and useless to future readers with similar problems, I am reversing. For another question in the same code, ask another question. Your error is the same as the previous question, with the file indice = fopen(...) == NULL. Are you doing indice = 0 and causing the crash in fprintf.

Show 2 more comments

Browser other questions tagged

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