Custom File Formats

Started by
7 comments, last by jim bob 23 years, 4 months ago
Hello. I was wondering, how do you make your own file formats that your program can read and process data with? I, just for test purposes want a file format with an extension of let''s say... *.bob. How can I make up a brand new file format, declare its type of data it processes, how much it can hold, what it does, etc. I am writing this in VC++, with C++ code. Any code would be greatly appreciated, as would tuts and websites. Again, this is just for test purposes, so it doesn''t have to be complex, just a basic file definition, format, and a program that can read data from it and write to it. Thank you VERY much guys. I am not worthy of a sig!
I am not worthy of a sig! ;)
Advertisement
I may not understand what you''re asking, but most file formats are fairly different from each other. My suggestion is to place a header at the top, and then any data it needs.

You can name a file anything and it will still function, so it doesn''t really matter how you do it, or what you name it, as long as it works.

Make a file named jim.txt, and write ABC in it. Then rename it jim.bmp. Start notepad and open that file, it will still have ABC in it. Of course, using paint, it will recognize that that isn''t really a bitmap since it has no header .


http://www.gdarchive.net/druidgames/
I know that files and file formats seem sort of mystic at the beginning, but you have to remember that a file is just a collection of data. Just like you have variables stored in RAM while your program is running, you can write those variables to disk in a file, and retrieve them a later time back into RAM from the file. Since you say you have Visual C++, I recomment looking up fopen, fclose, fread, and fwrite in the MSDN docs, or if you have any books, stuff about files should be in the books too.

Just remember that all you''re doing is calling functions to write your variables to disk, or read them from disk, or open/create files, or close files.

Also, like Null and Void said, if you are designing a format for any good use, you should include a header with info such as format version number, size of data/number of items, etc. From there it''s just copying variables.

Good luck!
Hey, thanks a lot guys, that does help.

But one more small request if you don''t mind...

Could someone please post some code (C++, Win32 Console App) that would load a made up file format (whatever you want to call it, doesn''t matter) into the program, and read or write to it or whatever. Also, the code to a header that defines that file type, to make it a legal type would also be nice. I hope you get what I mean. I just want to test loading a custom (text based) file format into a program, sort of for fun, but more to learn something. I learn much better when I can see code So, thanks for what you did post, and I really appreciate if anyone can help me with this small favor, thanks a lot!

I am not worthy of a sig!
I am not worthy of a sig! ;)
Maybe you''ll understand this:

File formats arent written in stone. I could write "Hello World!" to a file named foobar.bmp, and it might not open in notepad, but it would open if i made a function to specifically open it. Get it? It doesnt matter what you write, it matters how you interpret it, think of binary. Do you know what 0101 0101 1101 means? It could be a big number, a place in memory, etc., its just how you intpret it. Now if you know the C''s File I/O system, you should be able to write up any file, headers are ussually structs, and then write the data.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

The Micro$haft BSOD T-Shirt
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
Let''s say you were writing a file format to store compressed 16-bit image data. The header might look like:

typedef struct tagPICHEADER {
DWORD HeaderVal;
DWORD Width, Height;
DWORD CompLen, DeCompLen; // length of data when compressed / decompressed
} PICHEADER;

To read the data, you would read a PICHEADER from the file, check that HeaderVal is equal to, say, 828598640 (''pic1'', where 1 is the version number), then allocate CompLen bytes and read that amount of data into the buffer. Then you would close the file and attempt to decompress the data. You would already know how long the decompressed length was from looking at DeCompLen.

When writing the data, you would fill out a PICHEADER, setting HeaderVal to 828598640, then compress the image data, blah blah blah.

Overall, a file format is whatever to make it. Consistency between the reading and writing processes is the only thing that will make a file format work.
Several weeks ago, I wrote a program that packed .bmp files together in a "custom" filed I made up ".gfx". The method is just simple, open the .bmp files and read it into you own file.
To make something a legal type? As in with Windows? I''ll explain how that works real quick...

In the registry (in Windows, hit start->run and then type regedit, be careful with it if you don''t know how to use it!).

The first key (looks like a folder) is HKEY_CLASSES_ROOT, open it. Look for a key named .txt, open it and look at the default value, it should be something like txtfile.

Now look for a key named txtfile, open it, and look at the default value again. The default value should be something like Text Document, this is where/how Windows (98 in my case) looks up the type of file a certain extension is.

Now, open the key named shell from where you are. There is a list of applicable commands that files have associated with them here. Open the key named open, and the key within it named command. The default value in this key is the one that is associated with a txt file''s open command.

Does that help any?


http://www.gdarchive.net/druidgames/
Ahhh, yes. Ok, I get it now. Thanks a lot guys, I was wondering where that was going. Thanks, it does help.

I am not worthy of a sig!
I am not worthy of a sig! ;)

This topic is closed to new replies.

Advertisement