# Thread: Binary file write by user input then printing binary file data out

1. ## Binary file write by user input then printing binary file data out

following is the program I wrote it basically takes 9 inputs and then save them into binary file. then print out the data stored in binary data and find inverse of it then print the inverse out. but its stuck in a loop somewhere. can anyone help please. and tell me whats wrong;

Code:
```#include <stdio.h>

int main() {
int a[3][3],i,j;
float determinant=0;
int x;
FILE *fp = fopen ("file.bin", "wb");

printf("Enter upto 16 elements: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
fwrite (&x, sizeof (x), 1, fp);
fclose(fp);

printf("\nThe matrix is\n");
fopen("file.bin","rb");
while(!feof(fp))
{
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
}

for(i=0;i<3;i++)
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));

printf("\nInverse of matrix is: \n\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
printf("\n");
}
fclose(fp);

return 0;
}```

2. You not testing for end-of-file correctly. The fread should be in the while condition like this
Code:
`while (1 == fread (&x, sizeof (x), 1, fp))  // fread returns the number of elements read`

3. I just changed the code. the whole program I am trying to write is above. Sorry I was writing program in several parts just assembled all of it.

4. Originally Posted by Rahul Chawla
I just changed the code. the whole program I am trying to write is above. Sorry I was writing program in several parts just assembled all of it.
Wow.You've completely changed it from what you originally posted >.<

In this new version you're not reading anything from the file at all!

5. You're missing some braces in your first double-loop.

You're not assigning the return value of the second fopen to a variable!

You're not calling fread at all in the second one! And my first answer still applies there.

6. I will repost it back. let me spend some more time on it. Sorry mate.