# error when writing binary VTK files

**URL:** https://discourse.vtk.org/t/error-when-writing-binary-vtk-files/4487
**Category:** Support
**Tags:** file-formats
**Created:** [October 26, 2020, 8:25am UTC](https://discourse.vtk.org/t/error-when-writing-binary-vtk-files/4487 "2020-10-26T08:25:57Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![qzxuhui](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/q/d78d45/32.png) [@qzxuhui](https://discourse.vtk.org/u/qzxuhui)
#### Post date: [October 26, 2020, 8:25am UTC](https://discourse.vtk.org/t/error-when-writing-binary-vtk-files/4487/1 "2020-10-26T08:25:57Z")

</div>

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.

```auto
#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:

```auto
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

- [https://stackoverflow.com/questions/10913666/error-writing-binary-vtk-files](https://stackoverflow.com/questions/10913666/error-writing-binary-vtk-files)
- [https://stackoverflow.com/questions/55829282/write-vtk-file-in-binary-format](https://stackoverflow.com/questions/55829282/write-vtk-file-in-binary-format)
- [https://stackoverflow.com/questions/10608217/how-to-write-ascii-and-binary-data-to-the-same-file-at-the-same-time](https://stackoverflow.com/questions/10608217/how-to-write-ascii-and-binary-data-to-the-same-file-at-the-same-time)

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

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [October 26, 2020, 8:58am UTC](https://discourse.vtk.org/t/error-when-writing-binary-vtk-files/4487/2 "2020-10-26T08:58:35Z")

</div>

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)?

---

<div class="post-metadata">

### Author: ![qzxuhui](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/q/d78d45/32.png) [@qzxuhui](https://discourse.vtk.org/u/qzxuhui)
#### Post date: [October 26, 2020, 9:52am UTC](https://discourse.vtk.org/t/error-when-writing-binary-vtk-files/4487/3 "2020-10-26T09:52:46Z")

</div>

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

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [October 26, 2020, 10:33am UTC](https://discourse.vtk.org/t/error-when-writing-binary-vtk-files/4487/4 "2020-10-26T10:33:04Z")

</div>

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 🙂 .

---

<div class="post-metadata">

### Author: ![qzxuhui](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/q/d78d45/32.png) [@qzxuhui](https://discourse.vtk.org/u/qzxuhui)
#### Post date: [October 26, 2020, 10:37am UTC](https://discourse.vtk.org/t/error-when-writing-binary-vtk-files/4487/5 "2020-10-26T10:37:12Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [October 26, 2020, 10:40am UTC](https://discourse.vtk.org/t/error-when-writing-binary-vtk-files/4487/6 "2020-10-26T10:40:20Z")

</div>

Is too much to say that I am on your teacher 🙂  
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](https://people.math.sc.edu/Burkardt/data/vtu/vtu.html)

---

<div class="post-metadata">

### Author: ![qzxuhui](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/q/d78d45/32.png) [@qzxuhui](https://discourse.vtk.org/u/qzxuhui)
#### Post date: [October 26, 2020, 10:49am UTC](https://discourse.vtk.org/t/error-when-writing-binary-vtk-files/4487/7 "2020-10-26T10:49:20Z")

</div>

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. 😭

---

<div class="post-metadata">

### Author: ![qzxuhui](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/q/d78d45/32.png) [@qzxuhui](https://discourse.vtk.org/u/qzxuhui)
#### Post date: [October 26, 2020, 12:13pm UTC](https://discourse.vtk.org/t/error-when-writing-binary-vtk-files/4487/8 "2020-10-26T12:13:44Z")

</div>

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`

---

<div class="post-metadata">

### Author: ![cean\_wang](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cean_wang/32/2570_2.png) [@cean\_wang](https://discourse.vtk.org/u/cean_wang)
#### Post date: [January 3, 2021, 10:42am UTC](https://discourse.vtk.org/t/error-when-writing-binary-vtk-files/4487/9 "2021-01-03T10:42:14Z")

</div>

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](https://discourse.vtk.org/t/trying-to-write-vtu-files-from-fortran/470)
