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;
}