What methods are available to programatically look at a file and tell what type of file it is, for example mp3, txt, binary, photoshop, etc.? I don’t think there would be anything in the STL (except for trying to open a text file and read it in).
How does one go about trying to tell what types of files you are really dealing with? I mean I could put a .mp3 extension on a file that really isn’t an mp3 file.
2
Answers
Every file type has some sort of “signature,” a way to look at the file and identify its structure. In many cases, files start with a specific sequence of bytes. For example, PDF files start with a header, that looks like this:
There is no way around than reading the magic number of the file from its header, if you want to be sure. These are the bytes at the beginning. PNG files for example start with
PNG
.http://en.wikipedia.org/wiki/Magic_number_(programming)