1

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:

This is what I should be getting:

And this is what I'm getting:

My output

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:

enter image description here

And btw, my data is unsigned.

7
  • you're using int32 when you should use unsigned char. show the declaration of matrix Commented Nov 24, 2018 at 17:38
  • int ** 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. Commented Nov 24, 2018 at 18:37
  • Sorry @toohonestforthissite English is not my first lenguage and it has led to me using the term "2D array" wrongly. I'm using an int** matrix, and I want to write the content of it in a binary file. Commented Nov 24, 2018 at 18:44
  • @Sefean A matrix is a synonym for a 2D array here. Please read the second half of my previous comment, which says if you want a matrix/2D array, you should use one. Don't use inappropriate data structures. Commented Nov 24, 2018 at 18:47
  • @toohonestforthissite The thing is I strictly need to use a pointer to pointer, because later this will be used in MPI wich will require me to send each "line" of the matrix, one by one, that's why I'm using pointer to pointer. Commented Nov 24, 2018 at 18:49

1 Answer 1

2

matrix[i] is a pointer on 32-bit integers. Even if you assign values that hold in 8 bits, that doesn't "compress" the data when you're writing to a binary stream (plus you probably don't write the 512 values but only 512/4 = 128 values)

Your machine is little endian, so you get the LSB first, then 3 zeros, for each value.

So change the type of matrix[i] from int32_t * to char * or unsigned char * depending on what you need, make sure your values are in 8-bit range, and you can use fwrite like you're planning to.

If you cannot change the data type of matrix, use a loop to convert the values

for(i = 0; i < SIZE; i++)
{
    uint8_t to_write = matrix[i];  // assign/truncate for 0/255 range
    fwrite(&to_write, 1, 1, f);
}   

if your data is signed, you have to use int8_t. But in that case, you're not going to be able to write a0 as 160.

EDIT: your edit shows the real type of matrix. matrix[i] is a pointer on data, so you need to use a double loop to dump it, else you're copying the address of the array, not the value

for(i = 0; i < SIZE; i++)
{
   const int *row = matrix[i];
   for(j = 0; j < SIZE; j++)
   {
       uint8_t to_write = row[j];  // assign/truncate for 0/255 range
       fwrite(&to_write, 1, 1, f);
   }
}   
Sign up to request clarification or add additional context in comments.

5 Comments

I'm gonna answer you in the question so you can see the code properly.
uint8_t or int8_t would be other options since int32_t was in use.
@JonathanLeffler I get the same problem.
First issue to resolve is to use a 2D array, not a pointer to pointer.
oh, welcome back :) yes, OP edited. So I edited as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.