在C ++中创建与文件扩展名关联的自定义数据结构 [英] Creating a custom data structure associated with a file extension in C++

查看:127
本文介绍了在C ++中创建与文件扩展名关联的自定义数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我想创建一个自定义数据结构。



说,我有一个文件扩展名,myFile.alpha



这里alpha是文件延期。我理解一般的数据结构概念,如struct,union等。但我想学习如何创建文件扩展名和与之关联的数据结构,以便我的应用程序可以打开它。



如果有人可以提供任何例子,那就太棒了。



我试过的:



struct employee {

char * name;

int jobId;

}



这是一个非常简单的例子,但如果我看一下wav文件的数据结构,里面有一些叫做块的东西,一些元数据。据我所知,元数据对于每个文件都很常见。我可以做出的一种天真的结论是,文件扩展名只不过是一个带有一些元数据的文件。但只有我的应用程序才能打开它。这是真的还是还有更多的东西?

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 ?

推荐答案

对不起,问题,如同制定,没有意义,因为没有这样的事情。



由于历史原因,文件扩展主要用于Microsoft平台,目前可以被其他操作系统中的某些环境所尊重。它们与数据结构无关,一般情况下,甚至与某些文件格式无关;它们与系统中安装的某些应用程序相关联。



(另外,理解基本上没有文件名是很好的扩展。这是历史条款,其历史可以追溯到名称末尾的点后面的0到3个字母部分实际上不是名称的一部分,而是文件系统已知的单独字符串目前,所有这些文件系统都已过时;可以通过现代软件访问,但前者的扩展部分仅被视为某些命名约定的一部分。只有一个文件名,没有任何特殊部分。在Microsoft系统中,扩展通过系统注册表中写入的一些记录为shell所知。)



你能看到差异吗? 文件类型只是将文件名传递给某个应用程序的标准。操作系统不会知道有关文件格式的任何信息。而且,对于相同的扩展,格式可以完全不同。例如,将.html文件重命名为.docx并通过MS Word打开。它将起作用,即使格式在结构上是不相关的。单词知道如何识别这些格式,就是这样。媒体文件也是如此。例如,没有.MPG,.MP4或.MPEG文件格式。 MPEG-4是许多标准的集合,并且不同的流可以打包在多种不同类型的媒体容器中。某些应用程序可以识别其中一些格式,而不是支持或不支持内部的不同流格式。但是不要认为它是通过签名和元数据以常规方式完成的。通过读取文件的一些初始部分,没有任何通用标准用于按格式对文件进行分类。并非所有文件都有签名,并非所有文件都有元数据。应用程序只是尝试通过某些内部规则识别一个或另一个文件。在使用您自己的文件格式的应用程序中,您必须执行相同的操作。没什么大不了的,相信我。



不幸的是,你没有标记你正在使用的操作系统和语言,但最有可能的是你使用的是Windows和C ++。因此,您可能需要在应用程序中注册文件类型。最好和唯一完全一致的方法是安装。我强烈推荐开源WiX工具集,这是Microsoft技术最合法的产品,特别是现代的MSBuild标准和Visual Studio。对于此工具集,文件类型的注册是标准杂务之一:

WiX - Wikipedia,免费的百科全书 [ ^ ],

WiX工具集 [ ^ ]。



在许多情况下,不需要安装包,但注册文件类型的需要使其变得重要。最重要的部分是你必须提供卸载应用程序的选项并从系统注册表中删除所有安装痕迹。



参见:

https://msdn.microsoft。 com / zh-CN / library / windows / desktop / dd758090%28v = vs.85%29.aspx [ ^ ],

文件关联的最佳实践(Windows) [ ^ ]。< br $>


-SA
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:
https://msdn.microsoft.com/en-us/library/windows/desktop/dd758090%28v=vs.85%29.aspx[^],
Best Practices for File Associations (Windows)[^].

—SA


这篇关于在C ++中创建与文件扩展名关联的自定义数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆