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

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    7

    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;
    }
    Last edited by Rahul Chawla; 12-06-2013 at 11:03 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    7
    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. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Quote Originally Posted by Rahul Chawla View Post
    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. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Dec 2013
    Posts
    7
    I will repost it back. let me spend some more time on it. Sorry mate.

  7. #7

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I write binary to a file?
    By Jerry900 in forum C Programming
    Replies: 3
    Last Post: 12-10-2012, 06:29 PM
  2. Replies: 9
    Last Post: 12-05-2012, 01:11 PM
  3. Replies: 12
    Last Post: 06-18-2012, 08:23 AM
  4. How to write image data to binary PGM file format(P5)?
    By tommy_chai in forum C Programming
    Replies: 6
    Last Post: 11-03-2007, 10:52 PM
  5. write in a binary file
    By singersinger in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2007, 03:24 AM