Can anyone explain file extensions to me? Do they actually affect the file itself?

fuzzybabybunny

Moderator<br>Digital & Video Cameras
Moderator
Jan 2, 2006
10,455
35
91
I'm not too sure where to put this thread, but I guess in the interest of making programs that read certain files...

What are file extensions and how do they affect the file itself?

For example, I can take image.jpg and rename it to image.png and photo applications will still open up the file just fine.

If I rename image.img to image.dmg (an Apple-only file type) am I actually changing the contents of the file in any way? Am I somehow damaging the file by renaming it?

How do programs know how to open different file types?
 

fuzzybabybunny

Moderator<br>Digital & Video Cameras
Moderator
Jan 2, 2006
10,455
35
91
http://www.bleepingcomputer.com/tutorials/what-is-a-file-extension/

The file itself is not changed by changing the file extension.
OK, so file extensions are only there for the OS. The OS looks at the file extension, looks for a program that has been registered to read that file extension, and opens the program, passing in the file as an argument (supposedly).

How does a program such as, say, Windows Photo Viewer know how to read JPG vs PNG vs GIF vs etc? Are there headers at the top of the file that identify to the program what the file is and how it should read it?
 

sbpromania

Senior member
Mar 3, 2015
265
1
16
www.sbp-romania.com
How does a program such as, say, Windows Photo Viewer know how to read JPG vs PNG vs GIF vs etc? Are there headers at the top of the file that identify to the program what the file is and how it should read it?

The extension only relates to the programs that the OS uses in order to open a certain file. Nothing changes when changing the extension, only the default program that opens it.

For example, a PNG with transparency will remain transparent after changing its extension in JPG, something else or even delete it.
 

mv2devnull

Golden Member
Apr 13, 2010
1,526
160
106
Are there headers at the top of the file that identify to the program what the file is and how it should read it?
Yes - for some file formats, particularly binary formats.

No, a file does not tell how to read it; it has at most format identifier. The program has to know the format in order to read data properly.

A program may make assumptions based on the filename, particularly if the format lacks easily recognizable identifier within the contents.
 

mikeymikec

Lifer
May 19, 2011
20,985
16,232
136
Some programs will throw an error if the file extension does not match the file format data. Other programs very likely just throw each of its file format filters at the file until it runs out of filters or finds one that doesn't throw formatting errors.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,695
4,658
75
OK, so file extensions are only there for the OS. The OS looks at the file extension, looks for a program that has been registered to read that file extension, and opens the program, passing in the file as an argument (supposedly).
File extensions are there for whatever wants to read them. Changing a file extension is like taking a file that walks like a duck, quacks like a duck, and has a sign attached to it saying it's a ".duck", and changing that sign to read ".swan".

Are there headers at the top of the file that identify to the program what the file is and how it should read it?
Yes - for some file formats, particularly binary formats.

No, a file does not tell how to read it; it has at most format identifier. The program has to know the format in order to read data properly.

A program may make assumptions based on the filename, particularly if the format lacks easily recognizable identifier within the contents.

You may also be interested in http://linux.die.net/man/1/file, a command which reads magic numbers at the beginning of files.
 

Cogman

Lifer
Sep 19, 2000
10,286
145
106
And just an addition to what Ken g6 said. Different OSes do things differently when it comes to extensions.

For example, windows starts at the extension and works from there to determine what to do next. So if it runs into something with .exe, it will try to execute it (even though it will still parse the exe file to see how it should be executed. Fun fact, there are different exe standards). Windows pretty much always maps the extension to the program that is supposed to run it.

Linux, on the other hand, takes a much more mixed approach. Where no extension is present, linux relies heavily on magic numbers to determine what to do next. However, since windows set the precedent that not all files have magic numbers in them, there are many formats which linux has to rely on the extension to figure out what to do next.

It is a bunch of fun.
 

sm625

Diamond Member
May 6, 2011
8,172
137
106
You can take a jpg, rename it to a .bmp, and it will still open. The photo viewer/editor will basically just ignore the extension.
 

mpo

Senior member
Jan 8, 2010
458
51
91
Some file extensions are just custom versions of other types of extensions. Microsoft's file extensions for Office applications (.docx, .xlsx, etc.) are really xml containers.

You can change the extension from .docx to .zip and see the contents of the container with WinZip or 7Zip.

Since the xml is formatted, you can't just take any .zip and make it a .docx file.