设计和创建新的文件类型 [英] Designing and Creating New File Types

查看:105
本文介绍了设计和创建新的文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C / C ++ 中编程时,显然需要存储某些数据以供应用程序使用。为此,您将使用 RAM 。如果您的数据大小会增加或减少,您将使用不同的数据结构,如链接列表,树,阴影 ...等。



到目前为止一切都很好。但是当程序终止时,将清除存储在 RAM 中的数据。要重新加载数据,您需要将其保存到磁盘。



我的问题是:



如果您想将程序的数据写入文件,您如何实现这一目标? 设计文件类型的基础知识是什么?数据可以是;



~单个数据结构(可能是链表......)

~许多数据结构(也许4树结构)



MS Word,PowerPoint,Photoshop等程序如何实现这一目标?

When programming in C/C++ you will need to store some data for the use of your application, obviously. For this you''ll use the RAM. If the size of your data will be increased or decreased, you will use different data structures like linked lists, trees, hatches...etc.

Everything is just fine up to that point. But when your program terminates, that data, which is stored in the RAM will be cleared. To load your data back, you''ll need to save it to a disk.

My Question Is:

If you want to write your program''s data to a file, how can you achieve this ? What are the basics of designing a file type ? The data can be ;

~ A single data-structure (maybe a linked list..)
~ Many data-structures (maybe 4 tree-structures)

How do the programs like MS Word, PowerPoint, Photoshop... achieve this ?

推荐答案

您只需要为您的文件设计一个结构,该结构允许您编写数据,以便您可以将其读回并重新创建内存内容。如果您的数据是固定大小,那么您可以只写固定长度的块,如果它是可变的,那么您需要在每个记录的开头写入控制字段以显示它的长度。例如,一个简单的结构就像:

You just need to design a structure for your file that will allow you to write the data such that you can read it back and re-create the memory content. If your data is fixed size then you can just write in fixed length blocks, if it is variable then you need to write control fields at the beginning of each record to show how long it is. For example a simple structure would be something like:
Word0 : record type, some value indicating the content of this record.
Word1 : length, the size of the following data in bytes, or words as appropriate.
Word2 to N : data content
// repeated for all data



然后编写文件,你只需要查看所有数据记忆和每个项目,写入控制字,然后是长度,然后是实际数据。最后一条记录后面应跟一个表示数据结束的控制字。将其读回内存正好与此过程相反。


Then to write your file you just need to go through all the data in memory and for each item, write the control word, followed by the length, followed by the actual data. The last record should be followed by a control word that indicates end of data. Reading it back into memory is just the reverse of this process.


在处理文件时,保持以下拇指规则

1.收集您所需的所有数据自己定义的数据结构,如链接列表或结构等。

2.在退出程序之前,打开文件

3.将所有数据转储到文件中。 />
4.关闭文件



确保只打开文件一次写入并立即关闭。



通常,当我们进行文件操作时,在文件末尾写入校验和也很重要,因此每当文件内容更改时,都会更新校验和。它可以是CRC或MD5校验和。



通常,如果您使用基于Windows的应用程序,您可以定义自己的类型并使用您的应用程序加载您的类型的文件。您可以参考MSDN获取有关此
While processing with files, maintain following thumb rules
1. collect all the data required in your own defined data structure like, linked list or structures etc.
2. Before exiting the program, open the file
3. dump all the data to the file.
4. close the file

make sure that you open the file only once for writing and close it immediately.

Generally when we do file operations it is also important to write Checksum at the end of file, so whenever file contents changes update the checksum. it could be CRC or MD5 checksum.

Generally if you are working with windows based applications you can define your own type and load the files of your type with your application.you can refer MSDN for more details on this


这篇关于设计和创建新的文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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