Question: C program that will open a binary file. Arguments include file name and number of bytes on each row. output ex: ex output: $ ./hex
C program that will open a binary file. Arguments include file name and number of bytes on each row.
output ex:
ex output:
$ ./hex randomfile.bin 16 00000000 cf fa ed fe 07 00 00 01 03 00 00 00 02 00 00 00 00000010 0e 00 00 00 $ $ ./hex randomfile.bin 8 00000000 cf fa ed fe 07 00 00 01 00000008 03 00 00 00 02 00 00 00 00000010 0e 00 00 00
This is the code I am working with:
#include
int main(int argc, char *argv[]){ FILE *pFile; long lSize; char *buffer; size_t result; int i = 0;
pFile = fopen(argv[1], "rb"); if pFile == NULL{ fputs("File Error", stderr); exit(1); } fseek(pFile, 0, SEEK_END); lSize = ftell(pFile); rewind(pFile);
buffer = (char*) malloc(sizeof(char)*lSize); result = fread(buffer,1,lSize,pFile);
int n = strlen(argv[0]); for(i = 0; i < n; i++){ if((i % 16) == 0){ if(i != 0){ printf(" %s ", buffer); } printf (" %04x ", i); } printf(" %02x",result[i]); if(result[i] < 0x20 || result[i] > 0x7e){ buffer[i % 16] == '.'; } else{ buffer[i % 16] = result[i]; buffer[(i % 16) + 1] = '\0'; } } while ((i % 16) != 0) { printf (" "); i++; } printf (" %s ", buffer); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
