0

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

  • Welcome, you should paste a better code example : https://stackoverflow.com/help/mcve – aybe Jul 17 '18 at 17:37
  • 1
    Why not write the ascii string out as binary https://stackoverflow.com/questions/5664345/string-to-binary-in-c-sharp – 3dd Jul 17 '18 at 17:37

2 Answers2

1

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

Andre Motta
  • 759
  • 3
  • 16
0

You have missed using for StreamWriter and BinaryWriter. Change your code like this and it will work:

using (var fstream = new FileStream(..., FileMode.Create))
using (var writer = new StreamWriter(fstream))
{
    // Write headers
}

using (var fstream = new FileStream(..., FileMode.Append)
using (var writer = new BinaryWriter(fstream))
{
    // Write binary data
}

Also you can encode your headers to byte[] and use only BinaryWriter.

using (var fstream = new FileStream(..., FileMode.Create)
using (var writer = new BinaryWriter(fstream))
{
    // Write headers
    writer.Write(Encoding.ASCII.GetBytes("..."));

    // Write binary data
}

Or just use BinaryWriter to write string.

using (var fstream = new FileStream(..., FileMode.Create)
using (var writer = new BinaryWriter(fstream))
{
    // Write headers
    writer.Write("...");

    // Write binary data
}
Groxan
  • 748
  • 8
  • 19