Hi.
I want to read a matrix of 8-bit integers, in the range -127-to-128, into my program for further processing.
My code looks like like this:
When I print the matrix, I print the ascii values.Code:signed char* getdata(int rows, int columns){
FILE* InFile;
int n;
int elements = 0;
int siz = rows * columns;
int i = 0;
signed char* DataMatrix;
InFile = fopen("DWTinput.txt", "r");
DataMatrix = malloc(sizeof(char) * siz);
//Read matrix into array:
for(i = 0; i < siz; i++){
n = fscanf(InFile, "%c", &DataMatrix[i]); elements += n;
}
//Check if number of elements is correct:
if(elements != (siz)){
fprintf(stderr, "Mismatch in number of elements. Number was supposed to be %d, but is %d.", siz, elements);
goto error;
}
//Print matrix:
for(i = 0; i < siz; i++){
printf("%i ", DataMatrix[i]);
}
fclose(InFile);
return DataMatrix;
error:
return NULL;
}
How can I print the integer values?
Is it actually a good idea to use the 'char' datatype for processing integers (I should tell that I use the 'char' datatype to save memory space...), or should I use 'short int' instead?
Thanks,
Esben.