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?
Thank you very much, I didn’t know I should use the address for the second case.
– Vitor Figueredo
however I keep getting Segmentation fault, only now on the means of transport.
– Vitor Figueredo
Adjusted
nr_dias?– Guilherme Bernal
yes, I will update the code above with the changes.
– Vitor Figueredo
updated code.
– Vitor Figueredo
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.
– Vitor Figueredo
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 doingindice = 0and causing the crash infprintf.– Guilherme Bernal