I am very new to C and I have written a relatively simple code for an assignment that is supposed to take input from a file byte by byte and print a table of the hex values for the file similarly to the -od command. I moved all the code to a single file included here.
#include <stdio.h>
#include "dataDump.h"
//
// Created by crsan on 3/23/2023.
//
#include "dataDump.h"
int dataDump(char *fileName) {
FILE *inFile = fopen(fileName, "r");
if (inFile == NULL) {
return -1;
}
int whiteSpace = 00000000;
int line = 0;
unsigned char c;
printf("00000000");
char *hex = NULL;
for (int i = 0; (c = getc(inFile)) != EOF; i++) {
sprintf(hex, "%02x", c);
printf(" %s", hex);
whiteSpace++;
line++;
if (line == 15) {
printf("\n%08d", whiteSpace);
line = 0;
}
}
if (line != 0) {
printf("\n%d", whiteSpace);
}
return whiteSpace;
}
int main(int argc, char *argv[]) {
printf("test words");
dataDump(argv[1]);
return 0;
}
however, before even the print statement from the main function there is a segmentation error and the code fails to compile farther.
This is supposed to be compiled using UNIX command line input, which I am unfamiliar with and could be the cause of the error. Aside from that, google told me that it was probably a memory allocation error, which I have yet to learn about. Other possibility I was thinking could be the sprintf I use to change to hex. If anyone has encountered this before or has any idea what the cause might be any assistance would be greatly appreciated.