0

I'm making a checksum algorithm for one of my classes, I want to read two binary files and run them through a checksum algorithm. The checksum algorithm works (I've tried inputting what I want into the terminal and it works) but I can't get my fread() to work. I've tried printing the outputs and they print the correct stuff, but then a bunch of other random numbers and letters at the end.

Here is my code:

int main(int argc, char *argv[])
{
  FILE *ptr1;
  FILE *ptr2;

  ptr1 = fopen("test1.bin","rb");
  ptr2 = fopen("test2.bin","rb");

  char file1[sizeof(ptr1)], file2[sizeof(ptr2)];
  char sum[sizeof(ptr1)], comp[sizeof(ptr1)];

  fread(file1,sizeof(file1),1,ptr1);
  fread(file2,sizeof(file2),1,ptr2);

  fclose(ptr1);
  fclose(ptr2);



/* char file1[20], file2[20];
  char sum[20], comp[20];

  printf("enter 1\n");
  scanf("%s",&file1);
  printf("enter 2\n");
  scanf("%s",&file2);*/

  if(strlen(file1)==strlen(file2)) {
      char next='0';
      int length = strlen(file1);

      for(int i=length-1;i>=0;i--)
    {
      if(file1[i]=='0' && file2[i]=='0' && next=='0')
        {
          sum[i]='0';
          next='0';
        }
      else if(file1[i]=='0' && file2[i]=='0' && next=='1')
        {
          sum[i]='1';
          next='0';
        }
      else if(file1[i]=='0' && file2[i]=='1' && next=='0')
        {
          sum[i]='1';
          next='0';
        }
      else if(file1[i]=='0' && file2[i]=='1' && next=='1')
        {
          sum[i]='0';
          next='1';
        }
      else if(file1[i]=='1' && file2[i]=='0' && next=='0')
        {
          sum[i]='1';
          next='0';
        }
      else if(file1[i]=='1' && file2[i]=='0' && next=='1')
        {
          sum[i]='0';
          next='1';
        }
      else if(file1[i]=='1' && file2[i]=='1' && next=='0')
        {
          sum[i]='0';
          next='1';
        }
      else if(file1[i]=='1' && file2[i]=='1' && next=='1')
        {
          sum[i]='1';
          next='1';
        }
      else
        break;
    }

      for (int i=0;i<length;i++)
    {
      if(sum[i]=='0')
        comp[i]='1';
      else
        comp[i]='0';
    }

      if(next=='1')
    next='0';
      else
    next='1';

      printf("\nChecksum=%c%s",next, comp);
  }
  else {
    printf("\nInput Lengths do not match");
  }
}

test1.bin and test2.bin are two files that contain 8 bytes of binary. I've tried using

printf("this is file 1 %s\n", file1)
printf("this is file 2 %s\n", file2)

to help debug and it outputs

this is file 1 01001001dL
this is file 2 01001000P5L

What is my error here? I'm not great at C so I'm sure its something simple.

  • `sizeof(file1)` doesn't return the size of the file on disk; it gives the size in memory of `file1`, which is a pointer. You should pass the length of the file (or at least how much you want to read) as the second argument of `fread`. As it is you are likely reading only 4 bytes from each file. You're also doing `strlen` on binary data which might not be null-terminated which is a bad idea. – davmac Mar 27 '18 at 20:21

1 Answers1

0

You allocate sizeof(ptr1) bytes for file1, but that means the size of the type FILE*, which is likely to be 4. If you know your file contains exactly 8 bytes, do write 8 there.

Arndt Jonasson
  • 856
  • 1
  • 6
  • 14