Thread: Testing a binary file related program // Need help

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    12

    Testing a binary file related program // Need help

    Hi everyone! The following program has the purpose of writing in a binary file 65536 combinations of binary numbers with 16 digits. Furthermore, it also has to read from said file, given a certain combination in the line of commands, in which exact "line" is that combination located. For example, if you input "0000000000000111", it will print, "line 7". Just one more thing, i wrote two methods of finding the line, in which the first one does this without reading the file, whereas the second one is there to make sure it went to the right line, by reading the file.
    With that said, i'm having two major problems:
    1- The first one being that whenever i type "1111111111111111", the first process prints out line 65535. However the second one, using the fseek and fread functions prints 0... How can i fix this?
    2-Can someone tell me why is it that the binary file has around 130 kb of data?
    Thank you very much for the help, and as always if there is still some testing that needs to be done to make sure everything goes smoothly, please let me know!
    Here's the code:




    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main(int argc, char **argv)
    {
      unsigned short int w, i = 0;
      int c, b,p;
       // int a;
        FILE *f1;
    
    
        if(argc == 1)
          {
        printf("Input a certain configuration in the line of commands!\n");
        return 1;
          }
    
    
        f1 = fopen("Strings.bin", "wb+");
    
    
    
    
        for( i = 0; i < pow(2,16)-1; ++i)
          {
        fwrite(&i , sizeof(unsigned short int) , 1 , f1);
          }
    
    
        c = 0;
        i = 0;
    
    
         while (i < 16)
          {
              if(argv[1][15-i] == '1')
                    c += pow(2,i);
              ++i;
          }
    
    
        printf("The given input is located in the line: %d.\n", c);
    
    
        fseek(f1,c*sizeof(unsigned short int),SEEK_SET);
        fread(&w,sizeof(unsigned short int),1,f1);
    
    
        printf("Confirmation: %d\n",w);
    
    
        fclose(f1);
           return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your file should be exactly 65536×2 in length.

    You miss off the very last entry, so your fseek fails.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    12
    Right, yeah that makes sense. But how can i fix this, i went to check my file, and well, it contains around 128 kb of data... how come?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You write out 65536 values, and each value is 2 bytes.
    How many bytes do you write out?

    Also, using pow(2,n) to calculate powers of 2 is usually inefficient (and perhaps inaccurate) compared to 1<<n.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2017
    Posts
    12
    I understood the fact that my file should be around 130 kb, but if i'm writing 65536 values, being each 2 bytes, shouldn't my file be exactly 2 x 65536 bytes as opposed to 128 kb? Or is that related to the inefficiency associated with the calculations of the powers of 2?
    Just one final thing, being that i should have around 130 kb, how can i change the code for it to contain the last entry?
    Thanks in advance for the help!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You're not using windoes to guess the file size are you?
    Use the file properties to see exactly how many bytes there are.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by user_0x View Post
    I understood the fact that my file should be around 130 kb, but if i'm writing 65536 values, being each 2 bytes, shouldn't my file be exactly 2 x 65536 bytes as opposed to 128 kb?
    2**16 * 2 = 65536 * 2 = 131072 = 131.072kb = exactly 128 kib (where a kib is 2**10 = 1024)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-06-2013, 11:39 PM
  2. program crashes when copying binary file
    By bored_guy in forum C Programming
    Replies: 9
    Last Post: 11-21-2008, 03:02 AM
  3. Replies: 1
    Last Post: 12-10-2005, 11:25 AM
  4. student binary file program....
    By Newworld in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 08:50 PM
  5. Replies: 3
    Last Post: 03-31-2002, 11:03 AM

Tags for this Thread