Search code examples
c#vtkstreamwriterwritetofilebinarywriter

How to write Binary and ASCII to the same txt-file in c#?


as you can imagine I'm adressing my question to you as I wasn't able to solve my problem doing research. Also I'm rather unexperienced so maybe you can help.

At work I have a simulation program that produces huge amount of data so I want to write out every time increment as VTK file using binary. For reasons of acquiring the data I want to implement this vtkWrite by myself. Therefore as you might know, I need to write some lines in ASCII-format that contain the declaration of the data part that I need to write in binary format. Up to now my code looks like this:

    public void WriteVTKBin(int increment, string output_dir, bool format)
    {
        int data_structure = 12;
        int[,] ElementTypes = new int[hex8Elements.numberOfElements, 1];
        for (int ii = 0; ii < ElementTypes.Length; ii++) ElementTypes[ii, 0] = data_structure;
        string File_name = "output_bin_" + increment + ".vtk";
        var output = new FileStream(@output_dir + "/" + File_name, FileMode.Create);
        var output_Stream = new StreamWriter(output);
        using (output)
        {
            output_Stream.WriteLine("# vtk DataFile Version 3.0");
            output_Stream.WriteLine("vtk-Output for increment");
            if (format) output_Stream.WriteLine("BINARY");
            else output_Stream.WriteLine("ASCII");
            output_Stream.WriteLine("DATASET UNSTRUCTURED_GRID");
            output_Stream.WriteLine("POINTS " + nodes.numberOfNodes + " double");
        }
        var output_rest = new FileStream(@output_dir + "/" + File_name, FileMode.Append);
        var BinWriter = new BinaryWriter(output_rest);
        using (output_rest)
        {
            BinWriter.Write(GetBytesDouble(GetPointsOutput()));
        }
    }
 }

The argument taken from the BinaryWriter is a ByteArray that I produce using a different method. My idea was to initialize the file with FileMode.Create to overwrite potentially existing old files and then write the header section. Afterwards closing the file, open it again using FileMode.Append and writing my binary data. This I want to repeat until all fields I want to write out are contained in the vtk-file.

The Problem is: The BinaryWriter overwrites my header even though it's in Append-Mode and when I want to close it and write another ASCII-line it tells me that it cannot access an already closed file. So is there a solution for my approach or is there even a way more sophisticated way to deal with such types of output?

Thank you very much in advance,

RR


Solution

  • You can simply transform your ascii strings to binary and write them to file as such:

    ByteArray = Encoding.XXX.GetBytes(text)
    

    Where XXX is the encoding you want

    BinaryWriter.Write(ByteArray)
    

    When you open the file it will try to decode it with ascii and your ascii strings will be there for you to read