I want to get the Magic Number from a binary file (for example file ELF hava 7f 45 4c 46). I wrote a program to print out the magic number of the file but i get the error zsh: segmentation fault ./magic. How should I fix this?
int main()
{
//setlocale(LC_ALL, "Russian");
//FILE *fp;
//fopen (&fp, "/Documents/OCP/lab1test/lab1call", "rb");
FILE *fp;
long fSize;
fp = fopen("/Documents/OCP/lab1test/lab1call", "rb");
fseek (fp , 0 , SEEK_END);
fSize = ftell (fp);
rewind (fp);
char *magic_number;
magic_number=(char *)malloc(fSize+1);
//unsigned char magic_number[4];
fread(magic_number, sizeof(char), 4, fp);
printf ("A magic number of your file is:\n");
//magic_number[4] = '\0';
//for (int i = 0; i < 4; i++)
printf ("%02hhx%02hhx%02hhx%02hhx\n ", magic_number[0],magic_number[1], magic_number[2], magic_number[3]);
printf("\n");
}