Getting an error when triying to read a file: zsh: trace trap

54 Views Asked by At

I am writting a program in c that reads spells form a file and stores them in a "Feitico" type array, i am getting this error and i dont know what i am doing wrong.

The file will be like this:

Fire Bolt

0

Imagination

1 Action

120

0

0

Strange description (this spell fails to load anyway)

\EOD

Bless

1

Enchantment

1 Action

30

10

1

You bless up to three creatures of your choice within range. Whenever a target makes an attack roll or a saving throw before the spell ends, the target can roll a d4 and add the number rolled to the attack roll or saving throw.
At Higher Levels: When you cast this spell using a spell slot of 2nd level or higher, you can target one additional creature for each slot level above 1st.

\EOD

Each line will be an atribute of a spell, i want to be able to read those atrbutes and store them

Feitico* read_feitico_from(FILE *stream) {
    if (stream == NULL) return NULL;

    char nome[100];
    char castTime[100];
    int nivel;
    char escola[100];
    int alcance;
    int duracao;
    int concentracao;
    char description[500];
    description[0] = '\0';
    char buffer[500];
    
    if (fscanf(stream, "%99[^\n] %d %s %99[^\n] %d %d %d ", nome, &nivel, escola, castTime, &alcance, &duracao, &concentracao) == 7) {
        while (fgets(buffer, sizeof(buffer), stream) != NULL) {
            if (strcmp(buffer, "\\EOD\n") == 0) {
                break;
            }
            strcat(description, buffer);
        }

            
        Feitico *novoFeitico = create_feitico(nome, castTime, escola, nivel, alcance, duracao, concentracao, description);
        
        if (novoFeitico == NULL) {
            printf("Invalid Spell: %s\n", nome);
            return NULL;
        }
        else {
            return novoFeitico;
        }
    }
    return NULL;
}

Feitico** read_feiticos_from(FILE* stream, int* out_size) {
    if (stream == NULL || out_size == NULL) return NULL;
    *out_size = 0;
    int capacity = 100;
    Feitico** feiticos = malloc(sizeof(Feitico*) * capacity);
    if (feiticos == NULL) {
        printf("Erro de alocação de memória\n");
        return NULL;
    }
    Feitico* feitico;
    while (!feof(stream)) {
        feitico = read_feitico_from(stream);
        if (feitico != NULL) {
            printf("%d\n", *out_size);
            feiticos[*out_size] = feitico;
            (*out_size)++;
            if (*out_size >= capacity) {
                capacity += 100;    
                Feitico** temp = realloc(feiticos, sizeof(Feitico*) * capacity);
                if (temp == NULL) {
                    printf("Erro de realocação de memória\n");
                    for (int i = 0; i < *out_size; i++) {
                        free_feitico(feiticos[i]);
                    }
                    free(feiticos);
                    return NULL;
                }
                feiticos = temp;
            }
        }
    }

    // Print message if no valid spells were found
    if (*out_size == 0) {
        printf("Nenhum feitiço válido encontrado no arquivo\n");
    }
    return feiticos;
}

// THIs is the part of the main that will call the functions


        else if (strcmp("LOAD", comando) == 0) {
            char ficheiroParaLoad[40];
            scanf("%s", ficheiroParaLoad);

            FILE *file = fopen(ficheiroParaLoad, "r");
            if (file == NULL) {
                printf("Invalid file or error opening file!\n");
                return;
            } else {
                // Free existing spells
                for (int i = 0; i < numeroFeiticos; i++) {
                    free_feitico(feiticosLidos[i]);
                }

                // Load spells from file
                int out_size = 0;
                Feitico **feiticosLidosTemp = read_feiticos_from(file, &out_size);
                if (feiticosLidosTemp == NULL) {
                    printf("Error loading spells from file!\n");
                    return;
                }

                
                // Assign the newly loaded spells to feiticosLidos
                feiticosLidos = feiticosLidosTemp;
                numeroFeiticos = out_size;
                printf("%d\n", numeroFeiticos);

                // Close the file
                fclose(file);
            }
        }

For context a spell in the file will have this format:

Bless

1

Enchantment

1 Action

30 

10

1

You bless up to three creatures of your choice within range. Whenever a target makes an attack
 
roll or a saving throw before the spell ends, the target can roll a d4 and add the number rolled
to the attack roll or saving throw.

At Higher Levels: When you cast this spell using a spell slot of 2nd level or higher, you can target one additional creature for each slot level above 1st.

\EOD

This is the full output:

LOAD feiticos.sav

Invalid Spell: Fire Bolt

0

1

zsh: trace trap

"/Users/rafaellourenco/Downloads/Uni/VS/LP2/Spells2/"F_2

0

There are 0 best solutions below