*nbM is an int, but ftell returns a long int, so this may result in a loss of information:
This is wrong:
Code:
Msj=malloc((*nbM)*sizeof(int));
Msj is an unsigned char**, and furthermore it is a parameter. This overwrites whatever was passed as an argument, and then gets the allocation wrong by allocation a number of ints instead of a number of unsigned char pointers.
Looking at your loop, I think that what you want to do is to define the function like this, although I am wary of suggesting that you be a "three-star programmer":
Code:
CodigoError leer_archivo_txt(FILE* fpIn, unsigned char ***Msj, long int *nbM) {
int i;
fseek(fpIn, 0, SEEK_END);
*nbM = ftell(fpIn);
fseek(fpIn, 0, SEEK_SET);
*Msj = malloc(*nbM * sizeof((*Msj)[0]));
for (i = 0; i < *nbM; i++) {
(*Msj)[i] = malloc(sizeof((*Msj)[i][0]));
(*Msj)[i][0] = fgetc(fpIn);
}
}
but I don't really see the value of dynamically allocating space for just one unsigned char each time in the loop. Perhaps you do want an unsigned char** parameter after all (i.e., you want a dynamic array of unsigned char, but you need a pointer to it so that you can change it from within the function):
Code:
CodigoError leer_archivo_txt(FILE* fpIn, unsigned char **Msj, long int *nbM) {
int i;
fseek(fpIn, 0, SEEK_END);
*nbM = ftell(fpIn);
fseek(fpIn, 0, SEEK_SET);
*Msj = malloc(*nbM * sizeof((*Msj)[0]));
for (i = 0; i < *nbM; i++) {
(*Msj)[i] = fgetc(fpIn);
}
}
Note that you should check if malloc returns a null pointer, and if fgetc returns EOF.