i want to write a code to count occurrences of a specific hex number in a file. for example in the file there are 0x01 0x02 0x03 0x41 0x42 0x43 0x0D 0x0A 0xFF 0xFE 0xFD 0x01 0x02 0x03 0x80 0x7F 0x0D 0x0A and if i input : FF it will output: 1
i already working on the code but it doesn't seems to work
#include <stdio.h>
#include <stdlib.h>
main ()
{
FILE *in, *out;
unsigned char a[1000] = { 0 };
int b;
int count = 0, i = 0;
in = fopen ("a.dat", "rb");
out = fopen ("b.txt", "wb");
while (!feof (in)) {
b = fgetc (in);
a[i] = b;
i++;
}
scanf ("%x", &b);
for (i = 0; i < 1000; i++) {
if (a[i] == b) {
count++;
}
}
fprintf (out, "%d\n", count);
printf ("%d\n", count);
fclose (out);
fclose (in);
return 0;
}
(note: nesting error with '}' fixed)