I'm using this code to write the file:
FILE *f = fopen("out/solution.raw", "wb");
int i, j;
//SIZE = 512
for(i = 0; i < SIZE; i++)
{
fwrite(matrix[i], 1, SIZE, f);
}
fclose(f);
The problem is that when I open the file, it 3 '0' between the numbers, here are 2 screenshots to help you understand what I mean:
This is what I should be getting:
And this is what I'm getting:
As you can see my code is correctly writing each number, but there are three 0 between each number and I have no idea why.
I've also tried this:
fwrite(matrix[i], sizeof(matrix[i][0]), SIZE, f);
But none seems to work, any help would be greatly appreciated.
my matrix is declared as a 2D array of ints, as I need to do some operations with those numbers:
matrix = (int**)malloc(SIZE * sizeof(int*));
for (i = 0; i < SIZE; i++)
{
matrix [i] = (int*)malloc(SIZE * sizeof(int*));
}
I've tried your solution but I can't assign an unsiged char to an int, so I've tried casting it and I get this warning:
cast from pointer to integer of different size.
unsigned char to_write;
for(i = 0; i < SIZE; i++)
{
to_write = (unsigned char)matrix[i];
fwrite(&to_write, 1, 1, f);
}
(code used)
After that this is what I'm getting:
And btw, my data is unsigned.



int32when you should useunsigned char. show the declaration ofmatrixint **is not a 2D array of anything. It's a pointer to pointer, a completely different type. If you want a 2D array (i.e. an array of arrays), use one, there are enough examples how to do it right on SO and elsewhere.