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?
migrated from stackoverflow.com Aug 25 '12 at 10:37
This question came from our site for professional and enthusiast programmers.
If your 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 a 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 its a text file, and will not show you an image.
You also mentioned "this page". This page is transfered by HTTP from stackoverflow 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.