Wanzaz/Cryptocurrency-database

Differentiate capacity and length of the array

Wanzaz opened this issue · 0 comments

  • This program has only capacity of the array and take it as capacity
  • It uses MAXN instead of the length of the array
  • This the implementating:
TArrayOfCrypto * loadCryptocurrencies(FILE *file)
{
    TArrayOfCrypto * array = malloc(sizeof(TArrayOfCrypto));
    array->lenght = sizeof(TArrayOfCrypto);
    if (array == NULL) return NULL;

    array->value = NULL;
    array->lenght = 0;
    int i = 0;


    TCryptocurrency cryptocurrency;

    int checking;

    while(i < MAXN && 
            (checking = loadOneCrypto(file, &cryptocurrency, DATA_FORMAT)) == 6) {
        if (i == array->lenght) {
            array->lenght += BLOCK;
            TCryptocurrency * temp = realloc(array->value, array->lenght * sizeof(TCryptocurrency));
            if (temp == NULL) {
                return array;
            }
            array->value = temp;
        }
        array->value[i++] = cryptocurrency;
    }

    if (checking < 6 && checking >= 0) {
        printf("[SYNTAX ERROR]: while loading data from a database\n"
               "\tCorrect the format of the data\n");
        exit(-3);
    }

    array->value = realloc(array->value, i * sizeof(TCryptocurrency));
    array->lenght = i;
    return array;
}