Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
As the title says, I would like o create a custom data structure.

Say, I have a file extension, myFile.alpha

Here "alpha" is the file extension. I understand the general data structure concept like struct, union etc. But I would like to learn how to create a file extension and a data structure associated with it so that my application can open it.

If somebody can provide any examples it would be awesome.

What I have tried:

struct employee{
char *name;
int jobId;

Is a very simple example, but if I take a look at a data structure for say wav file, there's something called chunks contained inside, some meta data. As I understand meta data is common for every file. The kind of naive conclusion I can make is that, a file extension is nothing but a file with some meta data followed by something. But only my application will be able to open it. Is that true or is there something more ?
Updated 30-May-16 11:18am

1 solution

Sorry, the question, as formulated, makes little sense, because there is no such thing.

File "extensions" are used primarily on Microsoft platforms, by historical reasons, and presently can be respected by some kinds of environments in other OS. The are not associated with data structures, and, in general case, not even with some certain file formats; they are associated with some application installed in the system.

(Also, it's good to understand that, essentially, there are no file name "extension". This is the historical terms dated back to the time where 0 to 3-letter part of the name after dot at the end of the name was not really a part of the name, but a separate string known to the file system. Presently, all such file systems are obsolete; there can be accessed by modern software, but the former "extension" part is considered merely as a part of some naming convention. There is one single file name, without any special parts of it. The "extension", in Microsoft system, is known to the shell through some records written in the system registry.)

Can you see the difference? The "file type" is just the criterion for passing the file name to some application. The OS does not "know" anything about the file format. Moreover, formats, for the same extension, can be radically different. For example, rename a .html file to .docx and open in by MS Word. It will work, even though the formats are structurally unrelated. Words knows how to recognize those formats, that's it. Same thing about media files. For example, there is no such thing as .MPG, .MP4 or .MPEG file formats. MPEG-4 is a set of many standards, and different streams can be packed in a media container of a number of different types. Some application can recognize some of those formats and than support or not support different stream formats inside. But don't think that it is done on a regular way, through the signature and metadata. There is no any universal standards used to classify files by formats through reading some initial portion of the file. Not all files have signatures, and not all files have metadata. The applications just try to recognize one or another file by some internal rules. In your application using your own file format, you will have to do the same. Not a big deal, believe me.

Unfortunately, you did not tag the OS and language you are using, but it's most likely that your are using Windows and C++. So, you may need to register your file type with your application. The best and the only fully consistent way to do so is installation. I would highly recommend open-source WiX toolset, which is the most legitimate product for Microsoft technologies, especially modern MSBuild standard and Visual Studio. For this toolset, registration of file types is one of the standard chores:
WiX — Wikipedia, the free encyclopedia[^],
WiX Toolset[^].

In many cases, installation packages are not needed, but the need to register a file type makes it important. Most important part is that you have to give the option to uninstall the application and remove all traces of the installation from the system registry.

See also:[^],
Best Practices for File Associations (Windows)[^].

Share this answer
fashaa 30-May-16 18:17pm    
Thats a wonderful response. It cleared up what I was thinking about data structure and file extension, Spacibo !

So then, here's my question : I want to create a data structure that I want my application to open.

How can I create a data structure and save it to a file ? Lets say .mp3 or .wav or .rar whatever be it. From what you say OS registers the extension to pull the right application to open it. Before even registering something, I want to create a "data structure & write it to a file". Simply put thats what I am trying to do. If there's an example or something it would be great.
Sergey Alexandrovich Kryukov 30-May-16 18:29pm    
Пожалуйста! You are very welcome.

I would recommend you to develop a structure with some "likely-to-be-unique" file signature at the very beginning, followed by some metadata portion, followed by data. The rest of this question is too broad and vague to discuss seriously in another Quick Answer. The idea of using explicitly defined C++ structure to write it in the file is good. One note: you can use the serialization technique, learn about it. The detail of formats depend on your purpose.

If you have some particular concerns, please ask another question. If you want me to participate, please add a comment with a link, on the present comment.

For now, will you accept the present answer formally?

fashaa 30-May-16 18:43pm    
Here's something I have in my mind

struct metaData{
int someA;
int someB;
// etc

struct earth{

metaData *mD;
int mammals;
int birds;
bool intelligence;
char *names;
char *species;

Now that I have this, do I just write this i.e. earth struct to a file ?

int main(){

earth a;
// pass all info for earth structure
// Write to a file // Is this all ?
return 0;

If I write the struct to a file, it opens things like a text file ? May be a stupid question.

I will learn about "serialization". Do you recommend like a book or articles on this topic to deepen my knowledge. The requirements of my project is to write a known data type into a container so that it can be used to stream from memory faster. There are hell of a lot of read writes going on.
Sergey Alexandrovich Kryukov 30-May-16 18:49pm    
One of the advances approaches is offered by boost:

Another, "classical" approach is to add "symmetric" Load and Store method in each structure which read/write from/to C++ stream. You may need to use binary stream. If you would like with text files (which would need a lot more space), using plain text would be bad; then you should opt for XML or JSON serialization.

As I say, it worth a separate question. I think we first need to close the present one.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900