everytime I submit this code, I always encounter time limit exceeded. Any suggestion to fix the code?
this is the question:
Count the number of ASCII codes in a binary file. Enter a 2-digit hexadecimal number, count how many bytes in the binary file a.dat and whose value is exactly equal to the hexadecimal number, the statistical result must be written to the text in the format of "%d\n" in file b.txt. E.g: Let a.dat contain the following content (which has been converted to hexadecimal format for display):
0x01 0x02 0x03 0x41 0x42 0x43 0x0D 0x0A 0xFF 0xFE 0xFD 0x01 0x02 0x03 0x80 0x7F 0x0D 0x0A
###Input and output example:###
enter:
01
, output:2
(Because there are 2 0x01 in the file.)
enter:
FF
, output:1
(Because there is 1 0xFF in the file.)
this is the code:
#include <stdlib.h>
main(){
FILE *in,*out;
int a[100];
int b,n=0,count=0,i;
int *p=a;
in=fopen("a.dat","rb");
out=fopen("b.txt","wb");
while (fscanf(in,"%x",p++)!=EOF){
n++;
}
scanf("%x",&b);
for (i=0;i<n;i++){
if(b==a[i]){
count++;
}
}
fprintf(out,"%d\n",count);
fclose(out);
fclose(in);
return 0;
}