skip to Main Content

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


  1. 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:

    # xxd temp.pdf | head -n 1
    0000000: 2550 4446 2d31 2e33 0a25 c4e5 f2e5 eba7  %PDF-1.3.%......
    
    Login or Signup to reply.
  2. 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)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search