Problem with fgetc

1

I have a problem and that is that fgetc does not really read what is in the file. I have the following code:

int* readData(char* problem, int size) {
    FILE *f;
    //Obtenemos el fichero con el problema

    if ((f = fopen(problem, "r+")) == NULL){
        perror("Error, file doesn't exist");
        return NULL;
    }

    int n = size;

    int* problemArray = malloc ((n+1)*(n));
    int i;
    char data;

    //Libramos la primera posición
    fgetc(f);
    if (fgetc(f) - '0' != -38) {
        printf("Error, bad entry");
        return NULL;
    }

    //Obtenemos la representación en memoria del tablero
    for (i = 0; i < (n+1)*(n); i++){

        if ((data = fgetc(f)) == EOF){
            printf("Error, missing data\n");
            return NULL;
        }

        problemArray[i] = data - '0';
        printf("%c\n", data);


        if (problemArray[i] > (n-1) || problemArray[i] < 0){
            printf("Error, bad number %d + %d i\n", problemArray[i], i);
            //free(problemArray);
            return NULL;
        }

        printf("%d number\n", problemArray[i]);

        //Realizamos las correspondientes comprobaciones de formato
        data = fgetc(f);
        if((i % (n+1) == (n)) & (data - '0' != -38)){
            printf("Error, bad format1\n");
            //free(problemArray);
            return NULL;
        }

        if ((i % (n+1) != (n)) & ((data - '0' != -16) & (data - '0' != -48))){
            printf("Error, bad format2 + %d data + %d + i%ccadena\n", data-'0', i, data);
            //free(problemArray);
            return NULL;
        }

    }

    if (fgetc(f) != EOF){
        printf("Error, more numbers than predicted\n");
        //free(problemArray);
        return NULL;
    }

    return problemArray;
}

What happens is that if I pass an entry larger than 4x3, fgetc does not capture any number. For example:

   0 1 0 2 3

   1 0 1 2 3

   2 3 0 1 2

   3 3 0 1 2

It would give us the following output:

    0

    0 number

    1

    1 number

    0

    0 number

    2

    2 number

    3

    3 number

    1

    1 number

    0

    0 number

    1

    1 number

    2

    2 number

    3

    3 number

    2

    2 number

    3

    3 number

    0

    0 number

    1

    1 number

    2

    2 number

    3

    3 number

    3

    3 number

    0

    0 number

    Error, bad number -48 + 18 i

My problem is that it reads the -48 character, that is, the white character instead of reading the number 1. Someone could help me? Thanks

    
asked by Brais Castiñeiras Galdo 21.02.2018 в 19:51
source

1 answer

3

The problem is in an incorrect use of malloc() to which you have to pass the number of bytes you need, and not the number of elements . In particular, the line

int* problemArray = malloc ((n+1)*(n));

should be

int* problemArray = malloc ((n+1)*(n)*sizeof(int));

By not booking enough space, from a certain number of characters read you were starting to write outside the reserved area, with unpredictable results.

    
answered by 22.02.2018 / 11:14
source