error when writing binary VTK files

Hi everyone, I want to generate binary vtk file.

I write a code that can first generate correct ascii vtk file, then do some modification on that, let it can generate correct binary vtk file.

I face the problem when convert first acsii to binary, see the if switch in follow code.

#include <fstream>
#include <iostream>
#include "base64.h"
int main()
{
    std::ofstream fout;
    bool is_binary = true;
    fout.open("new.vtu",  std::ios::binary);
    fout << "<?xml version=\"1.0\"?>\n";
    fout << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n";
    fout << "<UnstructuredGrid>\n";
    fout << "<Piece NumberOfPoints=\"4\" NumberOfCells=\"2\">\n";
    fout << "<PointData Scalars=\"\" Vectors=\"\">\n";

    if (is_binary)
    {
        fout << "<DataArray type=\"Float64\" Name=\"solution\" NumberOfComponents=\"3\" format=\"binary\">\n";
        double x[] = {56.25, 6.25, 0, 100, 6.25, 0, 56.25, 0, 0, 100, 0, 0};
        int result_size;
        char *encoding = base64((char *)x, sizeof(double) * 12, &result_size);
        std::cout << result_size << std::endl;
        fout.write(encoding, result_size);
        free(encoding);
    }
    else
    {
        fout << "<DataArray type=\"Float64\" Name=\"solution\" NumberOfComponents=\"3\" format=\"ascii\">\n";
        fout << "56.25 6.25 0\n";
        fout << "100 6.25 0\n";
        fout << "56.25 0 0\n";
        fout << "100 0 0\n";
    }

    fout << "</DataArray>\n";
    fout << "</PointData>\n";
    fout << "<Points>\n";
    fout << "<DataArray type=\"Float64\" NumberOfComponents=\"3\" format=\"ascii\">\n";
    fout << "7.5 2.5 0\n";
    fout << "10 2.5 0\n";
    fout << "7.5 0 0\n";
    fout << "10 0 0\n";
    fout << "</DataArray>\n";
    fout << "</Points>\n";
    fout << "<Cells>\n";
    fout << "<DataArray type=\"Int32\" Name = \"connectivity\" format=\"ascii\">\n";
    fout << "0 2 1\n";
    fout << "1 2 3\n";
    fout << "</DataArray>\n";
    fout << "<DataArray type=\"Int32\" Name = \"offsets\" format=\"ascii\">\n";
    fout << "3\n";
    fout << "6\n";
    fout << "</DataArray>\n";
    fout << "<DataArray type=\"Int8\" Name = \"types\" format=\"ascii\">\n";
    fout << "5\n";
    fout << "5\n";
    fout << "5\n";
    fout << "5\n";
    fout << "</DataArray>\n";
    fout << "</Cells>\n";
    fout << "</Piece>\n";
    fout << "</UnstructuredGrid>\n";
    fout << "</VTKFile>\n";
    fout.close();
    return 0;
}

if is_binary = false, the code can generate correct vtk file, but if is_binary = true, the code can not generate correct vtk file.

I already notice that the base64 encoding, the base64.h is copied from https://github.com/superwills/NibbleAndAHalf/blob/master/NibbleAndAHalf/base64.h

If I open the vtk file with paraview, it complains that:

Can not read point data array "solution" for PointData in piece 0. The data array in the element may be too short

the code is compiled with g++ main.cpp, and my machine is little endian.

I have already see many topic

But I still can not figure out what I am doing wrong.
Can anyone help me? Thanks for your time.

Just an idea, I would open a valid vtu file to see the differences. By the way, have you tried to read/save the same file with vtk library (vtkXMLUnstructuredGridReader)?

Sir, I have zero experience in using vtk. I just have knowledge how to generate the vtk file.

So, in your case I would find a valid vtu file and I inspect it and I would try to follow that rules. If you think this path is good, please tell me if you found a vtu file, if you didn’t found, I’ll try to find one for you.

P.S. No need to call me ‘sir’, I am just a simple man :slight_smile: .

You are my teacher~ Great thanks bro. If you can find a simple vtu file with ascii and the corresponding version with binary, then I think I can handle the problem by myself.

Is too much to say that I am on your teacher :slight_smile:
I just try to help, if I can, that’s all.

You can find here few vtu files: https://people.math.sc.edu/Burkardt/data/vtu/vtu.html

Thanks for your sharing. The sadness is that, your example is all ascii, I can not understand what is the difference between acsii and binary version of vtu file. :sob:

Sir, I obtain the file by myself. Unlucky, I still can not understand the relationship between ascii file and binary file. Could you please give me some advice on this topic? https://discourse.vtk.org/t/how-to-convert-ascii-file-using-base64-encoding-to-binary-file/4489

Change this line:
fout << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n";
to:
fout << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\" header_type=\"UInt64\">\n";

This post has a binary demo file. Trying to write VTU files from fortran - Support - VTK