Just a curious question, how does a computer work out what file tyue a certain file is. E.G. How does a computer work out that this pages is a type of html? of each file, s there header information in the binary? Ram allocation? how does it know this?

  • 3
    For local files, it'll normally look for "magic" identifiers at (or at least toward) the beginning of the content. If it doesn't find that, it'll typically look at the name. For stuff off the web, MIME types are usually used to identify content type. – Jerry Coffin Aug 24 '12 at 21:34

If you are talking about an "extension", it is in the name of the file.

However, this information is used by the OS to launch a certain application, for example, Safari/Chrome/Firefox for files ending in .html.

The application then uses the header (first few bytes) of the file to see if can display it. It may also interpret it in a way that outputs junk data. For example, if you rename an image (.jpg) file with the extension .txt, the OS will try to open the default text editor (NotePad/TextEdit) to open it. The editor will try to interpret it as if it's a text file, and will not show you an image.

You also mentioned "this page". This page is transferred by HTTP from superuser.com to your browser. The HTTP header should mention that the data it is sending over is text/html. The browser will interpret it as HTML and render it. That said, browsers are very complex and forgiving. So, even if the header does not say what the type is, it will try to guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.