BrainInBlack
BrainInBlack

Reputation: 139

How to store data of different type's in one single file?

Problem

For my current project, i need to store images, serialized objects and so on, within a single file with an underlining file-structure. This whole procedure is new to me, because of my php background.

Question

What are good reads to that topic and what is the "best practice" for those kind of things?

Remark

The user should still be able to manipulate the data, i he/she likes to.

Upvotes: 2

Views: 503

Answers (3)

Steve Padmore
Steve Padmore

Reputation: 1740

For this you would use an Object Class with properties to contain the individual item types you need.

Create a web page/windows form etc. that loads the data for each of the Object's properties on screen, allowing the user to alter the data (text, numbers, images etc.).

Store these values in the Object Instance and then Serialize it (as Binary or XML for example), and save to a file.

You could then load the file and De-Serialize it into the Object Instance again, to display and allow editing.

However, just after seeing your edit...

How would a user edit an image in a text file? Are you certain that you will be storing Serialized data in a file that a user can edit manually?

Even using a zip file, the user could completely break any structure you may think is there. If you could expand on what they are storing and what part of the data they can edit and what those edits consist of, it might be easier to see your problem.

Upvotes: 1

arsenic87
arsenic87

Reputation: 51

When i need a "savefile" like this,first i store the type of the object in 1 byte, and then the object length in 4 bytes (binary int), and then the entire object in binary. If you want, you could add encryption on top of the serialized object and just code that in to your reader.

The System.IO.BinaryWriter and System.IO.BinaryReader are excellent for this.

First convert an int to 4 bytes, write it to your savefile, convert a enum value of the objecttype you are saving to a byte, write it to the file, then write the serialized object.

When opening the saved objects, just start at the beginning of the savefile, and you know you need to read 4 bytes to get the object length, one byte to get the type, and then just read as many bytes as you first read for length. Once done reading that object, the pointer in the savefile will be at a new 4 byte integer for the next object. Continue untill you reach the end of the file, and there you go :)

Upvotes: 0

DrKoch
DrKoch

Reputation: 9772

If you awnt the user be able to patch the files you should use some form of text file (as opposed to a binary file).

There are two very popular file formats used for tasks like yours: XML and JSON. For both file formats exist large and rich libraries.

Inherently binary data like pictures need to be serialized to some text format (like base64) before you can add it as a single element to such files.

These files are editable with a simple text editor.

Upvotes: 1

Related Questions