#include #include // tests // ./contains_byte_stdio examples/empty.txt 0x61 // ./contains_byte_stdio examples/lorem.txt 0x61 // ./contains_byte_stdio f 0xff // ./contains_byte_stdio f 0x0 int contains_byte(char *path, unsigned char byte); int main(int argc, char *argv[]) { if (argc <= 2) { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } unsigned char byte = strtoul(argv[2], NULL, 0); if (contains_byte(argv[1],byte)){ printf("Contains byte 0x%x\n", byte); } else { printf("Does not contain byte 0x%x\n", byte); } return 0; } // Return 1 of the given file contains the given byte. // Return 0 otherwise int contains_byte(char *path, unsigned char byte) { FILE *f = fopen(path, "r"); if (f == NULL) { perror(""); exit(1); } //read each byte and compare int curr_byte; while ((curr_byte = fgetc(f)) != EOF){ if (curr_byte == byte) { fclose(f); return 1; } } fclose(f); return 0; }