Zetto
Zetto

Reputation: 61

Program received signal SIGTRAP when I return 2 structs

Im trying to read these 2 files and store its content into 2 differents structs so my program dont need to be reading it over and over again, the problem is when I try to run both structs the program crashes, when I try to debug it, it shows the title's error, however if I try to run only the products file or the sells file it works fine and shows the result.

The code:

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

typedef struct sellsFile{
    int year, month, day, code;
    float sell_amount, price;
}sells;

typedef struct productsFile{
    int code;
    float stock_amount, uni_price, profit;
    char type;
}products;

productsFile *read_productsFile(int *q);
sellsFile *read_sellsFile(int *q);

int main(){
    int productsSize, sellsSize;
    products *v_products;
    sells *v_sells;

    v_products = read_productsFile(&productsSize);
    for(int cont=0;cont<productsSize;cont++){
        printf("%d;%c;%.2f;%.2f;%.2f;\n",v_products[cont].code, v_products[cont].type,v_products[cont].stock_amount,v_products[cont].uni_price,v_products[cont].profit);
    }
    printf("\n\n\n");

    v_sells = read_sellsFile(&sellsSize);
    for(int cont=0;cont<sellsSize;cont++){
        printf("%d/%d/%d    %d      %.2f        %.2f\n",v_sells[cont].year,v_sells[cont].month,v_sells[cont].day,v_sells[cont].code,v_sells[cont].sell_amount,v_sells[cont].price);
    }

}

productsFile *read_productsFile(int *q){
    (*q) = 0;
    FILE *archive;
    products *result;
    int code;
    float stock_amount, uni_price, profit;
    char type;

    archive = fopen("products.txt", "r");
    while(fscanf(archive,"%d;%c;%f;%f;%f;",&code,&type,&stock_amount,&uni_price,&profit) != EOF){
        (*q)++;
        result = (products *)realloc(result, sizeof(products) * (*q));
        result[(*q)-1].code = code;
        result[(*q)-1].type = type;
        result[(*q)-1].stock_amount = stock_amount;
        result[(*q)-1].uni_price = uni_price;
        result[(*q)-1].profit = profit;
    }

    return(result);
}

sellsFile *read_sellsFile(int *q){
    int year, month, day, code;
    float sell_amount, price;
    sells *result;
    *q = 0;
    FILE *archive;
    archive = fopen("sells.txt","r");
    while(fscanf(archive,"%d;%d;%d;%d;%f;%f;",&year,&month,&day,&code,&sell_amount,&price) != EOF){
        (*q)++;
        result = (sells *)realloc(result,sizeof(sells) * (*q));
        result[(*q)-1].year = year;
        result[(*q)-1].month = month;
        result[(*q)-1].day = day;
        result[(*q)-1].code = code;
        result[(*q)-1].price = price;
        result[(*q)-1].sell_amount = sell_amount;
    }
    return(result);
}

Example of produts file:

12100;P;17.400;2.30;38.80;
12200;P;25.000;23.70;13.58;
12300;P;16.090;17.48;12.75;

Example of sells file:

2015;1;1;15800;114.000;5.07;
2015;1;1;15600;9.000;9.79;
2015;1;1;12800;32.483;9.71;

Upvotes: 1

Views: 69

Answers (1)

Andrew Henle
Andrew Henle

Reputation: 1

Give the uninitialized value of result from this line of code:

sells *result;

this code results in undefined behavior:

    result = (sells *)realloc(result,sizeof(sells) * (*q));

as results starts with an unknown value.

Upvotes: 1

Related Questions