First of all the approach of this may differ depending on the file structure, but assuming that you know the boundaries of each section in the file or have some kind of binary data stored in the file to indicate the actual length of sections etc. IMO it would be way better if you didn't have the text stored like a normal text file by line, but rather as binary data.
The BinaryReader
/ BinaryWriter
classes (From System.IO
) will solve this problem the best, unless all the sections in your file have static sizes, then you can just use File.ReadAllBytes()
and simple copy the bytes from the byte array associated with the file. However assuming the sections have dynamic sizes then you might want to use something like this:
using (var fs = new FileStream("yourfile.bin", FileMode.Open))
{
using (var br = new BinaryReader(fs))
{
int sections = br.ReadInt32();
for (int i = 0; i < sections; i++)
{
int sectionLength = br.ReadInt32();
byte[] sectionData = br.ReadBytes(sectionLength);
// Use the data however you want ...
// A good idea would be to check whether it's text or an image
}
}
}
Which equals a file structure of the following:
4 bytes (int) for the amount of section
Each section would represent the following structure:
4 bytes (int) OR 8 bytes (long) if the images are big
byte[] DataBytes (This will either be the bytes of text or the bytes of images)
The same goes for writing to the actual file. Everytime you write data to the file you specify the size of the data before writing it.
This approach is also safer in the end.
Note: You could validate the data by either checking if the data has an image header or create your own kind of data header ex. 1 or 2 bytes for the type.
I'd sugges 2 bytes to have proper padding.
This could be an enum like the following:
enum DataType : short
{
Text = 0,
Image = 1
}
Then before reading the section data you read the type like:
var type = (DataType)br.ReadInt16();
This also makes it possible to easily expand the file structure with new different data structures. Ex. you could implement other things than just text and images, such as audio files, videos, other binary files etc.
If you have no knowledge of any of the data apart from maybe that the images have image headers then you might just want to compare the bytes and check for matching image headers. This may fail or may not as image headers may differ + you have no exact knowledge of the image data stored (Unless you actually read some of the header and gather the image boundaries then you could figure out how many bytes to read by logic. This is different depending on the image types ex. JPG, PNG, GIF etc. You could take a look at this: Getting image dimensions without reading the entire file