How does one create a file header in C, so that the file type can be detected when the header is read?
What is the correct way to do this, are there any standards to follow?
I would like to add a small header to my file so the file type can be detected when reading the header.
Update (if you don't have the hat):
I want to add a header for my own file format (not a .c or .h file), using C, and I will be using C to read the file, identify it and process it.
You could just write some custom data at the beginning of your file just like you would store any other data.
For example PGM format specifies that there are dimensions of picture and maximum value stored in first lines:
P2
# Shows the word "FEEP" (example from Netpbm main page on PGM)
24 7
15
... picture data continues from here
There are no standards that would specify making this kind of header since it is very rare to do such a thing. In case of PGM pictures you wouldn't know dimensions of picture without this header - you would read 12 bytes but you wouldn't know if it's picture 3x4 or 6x2...
Note that this kind of custom data is something that you have to expect to be stored at the beginning of the file when you are reading it. You can make up custom header for your files, but then make sure that people who are going to use your files know it.