Perform 2 passes through the file.
1 Scan and count the required bytes.
2 Highlight the required memory, then repeat the scan, this time save the results.
size_t ReadHexFile(FILE *inf, unsigned char *dest) { size_t count = 0; int n; if (dest == NULL) { unsigned char OneByte; while ((n = fscanf(inf, "%hhx", &OneByte)) == 1 ) { count++; } } else { while ((n = fscanf(inf, "%hhx", dest)) == 1 ) { dest++; } } if (n != EOF) { ;
source share