# Error while reading .vtk files with cpp

**URL:** https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156
**Category:** Support
**Created:** [April 7, 2023, 2:00am UTC](https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156 "2023-04-07T02:00:30Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Zhiying](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/97f17d/32.png) [@Zhiying](https://discourse.vtk.org/u/Zhiying)
#### Post date: [April 7, 2023, 2:00am UTC](https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156/1 "2023-04-07T02:00:30Z")

</div>

Hello all,  
I am using the C++ interface to read the cell-type arrays in the .vtk files. However, there exists a problem that I can only read one variable in the files. Here is the part of my codes.

```auto
std::string inputFilename = "TEST.vtk";

// Get all data from the file
vtkSmartPointer<vtkGenericDataObjectReader> reader =
    vtkSmartPointer<vtkGenericDataObjectReader>::New();
reader->SetFileName(inputFilename.c_str());
reader->Update();
reader->ReadAllScalarsOn();

// All of the standard data types can be checked and obtained like this:
if (reader->IsFileUnstructuredGrid())
{
    std::cout << "output is a polydata" << std::endl;
    vtkUnstructuredGrid* output = reader->GetUnstructuredGridOutput();
    std::cout << "output has " << output->GetNumberOfPoints() << " points." << std::endl;
    std::cout << "output has " << output->GetCellData()->GetNumberOfArrays() << " field arrays" << std::endl;
}

auto output = reader->GetOutput();
std::cout << " output has " << output->GetFieldData()->GetNumberOfArrays() << " fields data." << std::endl;

vtkSmartPointer<vtkDataSet> dataSet;
dataSet = ReadAnXMLFile<vtkDataSetReader>("TEST.vtk");
int numberOfCells = dataSet->GetNumberOfCells();

int numScalars = reader->GetNumberOfScalarsInFile();
std::cout << "output has " << numScalars << " Scalars" << std::endl;
for (int i = 0; i < numScalars; i++)
{
    const char* scalar_name = reader->GetScalarsNameInFile(i);
    std::cout << "scalar name is " << scalar_name << " Scalars" << std::endl;
    vtkTypeFloat64Array* gidData = vtkArrayDownCast<vtkTypeFloat64Array>(dataSet->GetCellData()->GetScalars(scalar_name));
    std::cout << "scalar name is " << scalar_name << " Scalars" << std::endl;
    std::cout << "array value is " << gidData <<std::endl;
    for (int i = 0; i < numberOfCells; i++)
        std::cout << "i = " << i << "value = " << gidData->GetValue(i) << std::endl;

}

```

The result of the program is that only one array is read, and all the other arrays have addresses of 0.

The vtk data format I use is as follows

```auto
# vtk DataFile Version 2.0
CoreMesh.vtk
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 129068 double
#information of points
CELLS 63869 574821
#information of cells
CELL_TYPES 63869 
#information of cell types
CELL_DATA 63869 
SCALARS variable1 double 1 
LOOKUP_TABLE table1 
#information of lookup table 1
SCALARS variable2 double 1 
LOOKUP_TABLE table2
#information of lookup table 2
SCALARS variable3 double 1 
LOOKUP_TABLE table3 
#information of lookup table 3

```

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [April 7, 2023, 9:29pm UTC](https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156/2 "2023-04-07T21:29:00Z")

</div>

Hi,

Can you, please, enclose the code between a ```cpp and a ```? This markup will render code much more readable.

thanks,

PC

---

<div class="post-metadata">

### Author: ![Zhiying](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/97f17d/32.png) [@Zhiying](https://discourse.vtk.org/u/Zhiying)
#### Post date: [April 9, 2023, 6:07am UTC](https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156/3 "2023-04-09T06:07:52Z")

</div>

Thank you for your reply. Is that what you mean?

```auto
std::string inputFilename = "TEST.vtk";

// Get all data from the file
vtkSmartPointer<vtkGenericDataObjectReader> reader =
   vtkSmartPointer<vtkGenericDataObjectReader>::New();
reader->SetFileName(inputFilename.c_str());
reader->Update();
reader->ReadAllScalarsOn();

// All of the standard data types can be checked and obtained like this:
if (reader->IsFileUnstructuredGrid())
{
   std::cout << "output is a polydata" << std::endl;
   vtkUnstructuredGrid* output = reader->GetUnstructuredGridOutput();
   std::cout << "output has " << output->GetNumberOfPoints() << " points." << std::endl;
   std::cout << "output has " << output->GetCellData()->GetNumberOfArrays() << " field arrays" << std::endl;
}

auto output = reader->GetOutput();
std::cout << " output has " << output->GetFieldData()->GetNumberOfArrays() << " fields data." << std::endl;

vtkSmartPointer<vtkDataSet> dataSet;
dataSet = ReadAnXMLFile<vtkDataSetReader>("TEST.vtk");
int numberOfCells = dataSet->GetNumberOfCells();

int numScalars = reader->GetNumberOfScalarsInFile();
std::cout << "output has " << numScalars << " Scalars" << std::endl;
for (int i = 0; i < numScalars; i++)
{
   const char* scalar_name = reader->GetScalarsNameInFile(i);
   std::cout << "scalar name is " << scalar_name << " Scalars" << std::endl;
   vtkTypeFloat64Array* gidData = vtkArrayDownCast<vtkTypeFloat64Array>(dataSet->GetCellData()->GetScalars(scalar_name));
   std::cout << "scalar name is " << scalar_name << " Scalars" << std::endl;
   std::cout << "array value is " << gidData <<std::endl;
   for (int i = 0; i < numberOfCells; i++)
       std::cout << "i = " << i << "value = " << gidData->GetValue(i) << std::endl;

}

```

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [April 9, 2023, 1:14pm UTC](https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156/4 "2023-04-09T13:14:09Z")

</div>

That’s it.

Well, can you post what you’re getting as output of your program?

best,

PC

---

<div class="post-metadata">

### Author: ![Zhiying](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/97f17d/32.png) [@Zhiying](https://discourse.vtk.org/u/Zhiying)
#### Post date: [April 9, 2023, 2:20pm UTC](https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156/5 "2023-04-09T14:20:28Z")

</div>

Thanks for your reply. Here is the output of my code.

```auto
output is a polydata
output has 129068 points.
output has 1 field arrays
           output has 0 fields data.
output has 4 Scalars
scalar name is RelPower Scalars
scalar name is RelPower Scalars
array value is 000002758B22D470
i = 0 value = 0
i = 1 value = 0
i = 2 value = 0
i = 3 value = 0
i = 4 value = 0
i = 5 value = 0
i = 6 value = 0
i = 7 value = 0
i = 8 value = 0
i = 9 value = 0
scalar name is CoolTemp(K) Scalars
scalar name is CoolTemp(K) Scalars
array value is 0000000000000000

process exited with error code -1073741819

```

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [April 9, 2023, 9:46pm UTC](https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156/6 "2023-04-09T21:46:55Z")

</div>

Hello,

I think the line below is doing an unsafe operation:

```cpp
gidData = vtkArrayDownCast<vtkTypeFloat64Array>(dataSet->GetCellData()->GetScalars(scalar_name));`

```

I wouldn’t assume the data array is a `vtkTypeFloat64Array`. Maybe you should query its underlying data type with the `IsA()`, `GetDataType()` and/or the `GetDataTypeAsString()` methods before casting that pointer. Check the docs: [https://vtk.org/doc/nightly/html/classvtkDataArray.html](https://vtk.org/doc/nightly/html/classvtkDataArray.html)

For example:

```cpp
<cassert>
<vtkType.h>

(...)

vtkDataArray* dataArray = dataSet->GetCellData()->GetScalars(scalar_name);

assert( dataArray && "Error in main(): data array not found (null pointer). Giving up..." );

if( dataArray->GetDataType() == VTK_DOUBLE){
   gidData = vtkArrayDownCast<vtkTypeFloat64Array>( dataArray );

   (...)

} else {
   assert( false && "Error in main(): data array of unsupported type encountered. Giving up..." );
}

```

take care,

PC

---

<div class="post-metadata">

### Author: ![Zhiying](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/97f17d/32.png) [@Zhiying](https://discourse.vtk.org/u/Zhiying)
#### Post date: [April 10, 2023, 2:11am UTC](https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156/7 "2023-04-10T02:11:38Z")

</div>

Thank you very much for your suggestion. I have tested according to your suggestion, but the result is still wrong. The program seems to be unable to read the address of the second scalar. Here is the code I used:

```auto
    int numScalars = reader->GetNumberOfScalarsInFile();
    std::cout << "output has " << numScalars << " Scalars" << std::endl;
    for (int i = 0; i < numScalars; i++)
    {
        const char* scalar_name = reader->GetScalarsNameInFile(i);
        std::cout << "scalar name is " << scalar_name << std::endl;

        vtkDataArray* dataArray = dataSet->GetCellData()->GetScalars(scalar_name);
        std::cout << "The data type of data array " << i << " is " << dataArray->GetDataTypeAsString() << std::endl;
        assert(dataArray && "Error in main(): data array not found (null pointer). Giving up...");
        if (dataArray->GetDataType() == VTK_DOUBLE)
        {
            std::cout << "Data type of scalar array " << i << " is VTK_DOUBLE" << std::endl;
            vtkTypeFloat64Array * gidData = vtkArrayDownCast<vtkTypeFloat64Array>(dataArray);
            for (int j = 0; j < 10; j++)
                std::cout << "j = " << j << " value = " << gidData->GetValue(j) << std::endl;
        }
        else
        {
            assert(false && "Error in main(): data array of unsupported type encountered. Giving up...");
        }
    }

```

And the program runs as follows

```auto
output has 4 Scalars
scalar name is scalar1
The data type of data array 0 is double
Data type of scalar array 0 is VTK_DOUBLE
j = 0 value = 0
j = 1 value = 0
j = 2 value = 0
j = 3 value = 0
j = 4 value = 0
j = 5 value = 0
j = 6 value = 0
j = 7 value = 0
j = 8 value = 0
j = 9 value = 0
scalar name is scalar2

Process exited with code -1073741819

```

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [April 12, 2023, 12:10pm UTC](https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156/8 "2023-04-12T12:10:16Z")

</div>

> [@Zhiying](#):
>
> ```auto
> std::cout << "The data type of data array " << i << " is " << dataArray->GetDataTypeAsString() << std::endl;
> assert(dataArray && "Error in main(): data array not found (null pointer). Giving up...");
> 
> ```

Please, swap those two lines and try again. The assert must come before all attempts to use the pointer.

---

<div class="post-metadata">

### Author: ![Zhiying](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/97f17d/32.png) [@Zhiying](https://discourse.vtk.org/u/Zhiying)
#### Post date: [April 12, 2023, 12:21pm UTC](https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156/9 "2023-04-12T12:21:56Z")

</div>

Thanks for your reply. I just tried as you suggested, and the output is as follows:

```auto
output is a polydata
output has 129068 points.
output has 1 field arrays
           output has 0 fields data.
output has 4 Scalars
scalar name is Scalar1
The data type of data array 0 is double
Data type of scalar array 0 is VTK_DOUBLE
j = 0 value = 0
j = 1 value = 0
j = 2 value = 0
j = 3 value = 0
j = 4 value = 0
j = 5 value = 0
j = 6 value = 0
j = 7 value = 0
j = 8 value = 0
j = 9 value = 0
scalar name is Scalar2
Assertion failed: dataArray && "Error in main(): data array not found (null pointer). Giving up...", file E:\cpp_pratice\vtk_test2\vtk_test2.cpp, line 99

process exited with code 3

```

In addition, I would like to ask is this error is possibly related to the large amount of data in the file?

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [April 12, 2023, 1:23pm UTC](https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156/10 "2023-04-12T13:23:26Z")

</div>

> [@Zhiying](#):
>
> `std::cout << "scalar name is " << scalar_name << std::endl;`

Please, change the above to

`std::cout << "scalar name is [" << scalar_name << "]" << std::endl;`

The assert message is telling exact what it says: you’re getting a null pointer when quering for a scalar field named “Scalar2”.

> [@Zhiying](#):
>
> I would like to ask is this error is possibly related to the large amount of data in the file?

I don’t think so, but it’s early to rule that out.

---

<div class="post-metadata">

### Author: ![Zhiying](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/97f17d/32.png) [@Zhiying](https://discourse.vtk.org/u/Zhiying)
#### Post date: [April 12, 2023, 1:43pm UTC](https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156/11 "2023-04-12T13:43:48Z")

</div>

> [@Zhiying](#):
>
> `int numScalars = reader->GetNumberOfScalarsInFile();`

The .vtk file I’m going to read contains four scalars. I use the code above to get the number of scalars and return a value of 4. But I can’t find an interface for reading scalars through the reader variable. So I had to create a dataSet to read the scalars using the following code:

```auto
vtkSmartPointer<vtkDataSet> dataSet;

```

Maybe there’s a better way for me to read scalars using some other interface?  
I have changed the output according to your suggestion, but the output has not changed much. The scalar name in the test VTK file I used is scalar+n

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [April 13, 2023, 10:19pm UTC](https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156/12 "2023-04-13T22:19:11Z")

</div>

> [@Paulo\_Carvalho](#):
>
> Please, change the above to
> 
> `std::cout << "scalar name is [" << scalar_name << "]" << std::endl;`

👆 👆 👆  
What did you get in std::cout?

---

<div class="post-metadata">

### Author: ![Zhiying](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/97f17d/32.png) [@Zhiying](https://discourse.vtk.org/u/Zhiying)
#### Post date: [April 14, 2023, 2:27am UTC](https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156/13 "2023-04-14T02:27:48Z")

</div>

```auto
scalar name is Power]
The data type of data array 0 is double
Data type of scalar array 0 is VTK_DOUBLE
scalar name is [Temp]

```

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [April 14, 2023, 1:04pm UTC](https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156/14 "2023-04-14T13:04:23Z")

</div>

Thank you. That test was to rule out extra spaces or characters in the name. I personally don’t like indexes based on strings much.

Why don’t you use `dataSet->GetCellData()->GetNumberOfArrays()` and `dataSet->GetCellData()->GetArray(int)` in a `for` loop to iterate over the `vtkDataArray`s? IMO, using integer indexing are preferable to string indexing.

---

<div class="post-metadata">

### Author: ![Zhiying](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/97f17d/32.png) [@Zhiying](https://discourse.vtk.org/u/Zhiying)
#### Post date: [April 15, 2023, 8:10am UTC](https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156/15 "2023-04-15T08:10:51Z")

</div>

Thanks very much for your reply. When I use `dataSet->GetCellData()->GetNumberOfArrays()`, its return value is 1, which is not equal to the actual number of arrays in the .vtk file.  
And output of `dataSet->GetCellData()->GetArray(int)` is as follows:

```auto
The number of arrays in dataSet is = 1
output has 4 Scalars
scalar name is [1]
The data type of data array 0 is double
Data type of scalar array 0 is VTK_DOUBLE
j = 0 value = 0
j = 1 value = 0
j = 2 value = 0

scalar name is [2]
Assertion failed: dataArray && "Error in main(): data array not found (null pointer). Giving up...", file E:\vtk_test2\vtk_test2.cpp, line 104

Process exited with code 3

```

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [April 15, 2023, 11:21am UTC](https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156/16 "2023-04-15T11:21:59Z")

</div>

> [@Zhiying](#):
>
> its return value is 1

VTK is telling you it found only one array, so attempting to fetch a 2nd array results in a null pointer as expected. I believe you could consider the hypothesis of an ill-formed `.vtk` file. The sample you posted further up doesn’t seem to be a complete file, so I can’t judge. Maybe you should try loading a trivial or one known to work. You can find some here with scalars: [VTK Files](https://people.sc.fsu.edu/~jburkardt/data/vtk/vtk.html) .

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [April 15, 2023, 11:50am UTC](https://discourse.vtk.org/t/error-while-reading-vtk-files-with-cpp/11156/17 "2023-04-15T11:50:54Z")

</div>

The reader reports the file has 4 scalars, yet `GetNumberOfArrays()` reports just one array. Perhaps the API is not doing what we’re think it does. Maybe there is some other way to iterate over the other scalars, via a different object, while each scalar has just one data array.

Here’s how I setup a multivariate data structure:

```cpp
        unstructuredGrid->GetCellData()->SetScalars( phi ); //phi (porosity) is a vtkDataArray
        unstructuredGrid->GetCellData()->AddArray( k ); //k (permeability) is a vtkDataArray

```

To use one scalar field, I have to “activate” it:

```cpp
      unstructuredGrid->GetCellData()->SetActiveScalars("Porosity");

```

Maybe you have do loop over the 4 scalar names like you did in first place, activate it with the `SetActiveScalars()` method, then fetch the single `vtkDataArray`. So, instead of doing:

```cpp
(...)
for(...){
   (...)
   ... = dataSet->GetCellData()->GetScalars(scalar_name);
   (...)
}
(...)

```

Try doing:

```cpp
(...)
for(...){
   (...)
   dataSet->GetCellData()->SetActiveScalars(scalar_name);
   ... = dataSet->GetCellData()->GetScalars();
   (...)
}
(...)

```
