writing custom files(with specific data structure)

The real issue i have is writing the data so that it follows the structure needed.
like:
3 byte header
20 byte string
20 byte string
20 byte string
4 bytes of null (00 in hex)

etc. It contains quite a lot of 00 bytes and i need to write them at right spots to keep the structure of the data - so the program that reads it would read at the right value.
Right now im using filestream and im simply writing byte arrays from input textboxes - and when the input for string is too short, a simple if statement will check that and create a new array of 00 bytes (like string is 12 bytes, but block for it is 20 so it creates 8 byte long 00 array).
Problem is combining the arrays:
[code] //dds1,dds2,dir + 00 values
header.CopyTo(endArray1,0);
txt1Array.CopyTo(endArray1, header.Length);
fillNull1.CopyTo(endArray1, header.Length + txt1Array.Length);
txt2Array.CopyTo(endArray1, header.Length + txt1Array.Length + fillNull1.Length);
fillNull2.CopyTo(endArray1, header.Length + txt1Array.Length + fillNull1.Length + txt2Array.Length);
txt3Array.CopyTo(endArray1, header.Length + txt1Array.Length + fillNull1.Length + txt2Array.Length + fillNull2.Length);
fillNull3.CopyTo(endArray1, header.Length + txt1Array.Length + fillNull1.Length + txt2Array.Length + fillNull2.Length + txt3Array.Length);
[/code]

and i end up making 3-4 of this kind of copyto arrays and then combine these again together (to avoid the length part getting insanely long).
And write to file:
[code] nStream.Write(finalArray, 0, finalArray.Length);[/code]
If also have to add a option to create multiple blocks that gets inserted one after another if user needs to. So the code should be more flexible. Like resetting the textboxes if the user selects add another block, while storing the values of first arrays in memory.

file is basically a config file for a game - the game reads it to get info for effects, indexes etc - i dont have access to its source code nether.

Comments

  • why don't you use an xml instead to write the config file you want as i understand you wanna to keep it updated and only the fields that the user checked will be written so it so much easier in xml here is a link if you are interested

    http://support.microsoft.com/kb/307548

    but if you still wanna to stick with your method by writing a binary file then you will simply need to use binaryReader and BinaryWriter classes instead of using an array then copy it here is an example :

    http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx

    hope this will help you.
  • xml wont work since the program it is for reads only this specific format and extension (.EFF).
    iv been workign on it meanwhile and one of the solutions i came up with was to copy the byte arrays to arraylist (so the whole coping and readign is in a separate method), when i have enough data in the array list, i can save it by by converting the arraylist to byte array and simply writing it.

    however, is there actually a method that would let me write to file at specific offsets? like starting at byte 56 etc.
  • Ok here is console apllication that will help you on how to handle binarywriter and how to go to the specified offset, you can play with it to get the idea.

    once the binaryfile created you can grap it and drop it in to VS.NET or any other hex editor program so you can see what is inside it.

    now when you use BinaryReader it is almost the same as BinaryWriter except you will use Read method instead of Write method that's it

    [code]

    using System;
    using System.IO;

    namespace BinaryFile
    {
    class Program
    {
    // Fields

    static int personId = 0x00;
    static string personName = null;
    static int secretNumber = 0xF1;
    static string secretString = "WontedWinter54";
    static int binLength = 0xFF;
    static string filePath = @C:TempBinaryFile.bin; // Make sure you have the Temp Folder in C:

    // Method.
    static void Main(string[] args)
    {

    // user interaction.
    Console.WriteLine("Welcome too...");
    Console.Write("Please enter your Id : ");
    personId = Convert.ToInt32(Console.ReadLine());
    Console.Write("Please enter your name : ");
    personName = Console.ReadLine();

    Console.WriteLine("Please wait....");

    // To create or open an existing file.
    using (FileStream stream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, binLength))
    {
    // Declare binarywriter to write everything.
    using (BinaryWriter writer = new BinaryWriter(stream))
    {
    // Write the person id in the first position
    // which is 0.
    writer.Write(personId);
    // Write the person name after the id in position 4
    // Integer32 takes 4 byte.
    writer.Write(personName);
    // Seek : to set the position in 64.
    stream.Seek(64, SeekOrigin.Begin);
    // write the secret number in position 64.
    writer.Write(secretNumber);
    // Seek : to set the position in 128.
    stream.Seek(128, SeekOrigin.Begin);
    // write the secret string in position 128.
    writer.Write(secretString);

    }
    }
    Console.WriteLine("Finish writing to the file.");
    }
    }
    }

    [/code]

    regards.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories