Hey guys. I just learn about magic numbers today. I am trying to make a program that uses fstream along with magic numbers to tell what a file is. Example the magic numbers for jpg are ff d8 ff e0. How do i check a file to see if its a jpg using these numbers. I want to read the first part of the file from my understanding of magic numbers. They should be at the top of the file. A simple example of how to read using magic numbers would be appreciated

Recommended Answers

All 4 Replies

There's nothing magic about reading your magic numbers.
1) Open the file in binary.
2) Read as many bytes as you need for your test.
3) Test the bytes for the correct values.
4) done...

You need to read the file in "binary mode". While you can probably do the same thing with fstream, I honestly have never bothered learning how. The C way to do it uses fread() on a FILE* object:

#include <stdio.h>
#include <iostream>
using namespace std;


FILE *fp = fopen("C:/Users/DFlo/Pictures/13.jpg", "rb");
unsigned char magic[4];
fread((void *)magic, 1, 4, fp);
cout << hex << "magic:";
for (int i = 0;  i < 4;  i++)
    cout << " 0x" << int(magic[i]);
cout << dec << endl;

Note that fopen() return NULL on failure, and fread() returns the number of items read (should equal 4 in this case).

i really want to learn the fstream way of reading the magic number. Anyone kno how to here is a code i have. juse dont know how to get the magic number.

# include <fstream>
# include <iostream>
# include <stdlib.h>
# include <string>
using namespace std;

int main()
	fstream filestr;
	unsigned char magic[4];
	//filestr.open("C:\\Users\\Public\\Documents\\project\\bio.pdf", fstream::in | fstream::binary);
	filestr << "\nThis is EPIC looking. \n";
	system("C:\\Users\\Public\\Documents\\project\\iTunes\\itunes.exe know.mp3");
	//system("C:\\Users\\Public\\Documents\\project\\WindowsMediaPlayer\\wmplayer.exe C:\\Users\\Public\\Documents\\project\\know.mp3");
	cout<<"Thanks for using this program"<<endl;
	return 0;

The problem with using magic numbers to identify a file format is that the numbers may be just coincidental to the rest of the file. For example I might have a binary file whose first few bytes are 49 49 2A 00, but that doesn't mean its a TIF file.

There are lots of magic numbers as shown in this wiki article.

In any event, I suppose you would want to check the magic numbers of a known file format to find out if the file is in that format. In the case of TIF the magia numbers occupy the first 4 bytes of the file, so you would use fstream to read the first four bytes

ifstream in("filename", ios::binary);
if( in.is_open())
   unsigned char magic[4] = {0};
   in.read(magic, sizeof(magic));
   if( magic[0] == 0x49 && magic[1] == 0x48 && magic[2] == 0x2a && pagic[3] == 0)
       // It's a hit!

I just tried this on a pdf file and the first few bytes are in text and read "%PDF-13".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.