gpt4 book ai didi

c++ - 写入二进制 VTK 文件时出错

转载 作者:搜寻专家 更新时间:2023-10-31 01:14:47 25 4
gpt4 key购买 nike

我正在尝试编写一个基本的二进制 VTK 文件以使用 ParaView 显示一些数据,但我有一些错误,我不明白为什么。这是我在 C++ 中的测试代码:

#include <iostream>
#include <fstream>

double myarray[72] = {
0,0,0,1,0,0,2,0,0,3,0,0,4,0,0,
5,0,0,0,1,0,1,1,0,2,1,0,3,1,0,
4,1,0,5,1,0,0,2,0,1,2,0,2,2,0,
3,2,0,4,2,0,5,2,0,0,3,0,1,3,0,
2,3,0,3,3,0,4,3,0,5,3,0};
int main()
{
std::ofstream vtkstream("test01.vtk", std::ios::out | std::ios::trunc);
bool ascii = false;
if (vtkstream) {
vtkstream<<"# vtk DataFile Version 2.0"<<"\n";
vtkstream<<"Exemple"<<"\n";
if (ascii) {
vtkstream<<"ASCII"<<"\n";
vtkstream.close();
vtkstream.clear();
vtkstream.open("test01.vtk", std::ios::out | std::ios::app);
vtkstream<<"DATASET STRUCTURED_GRID"<<std::endl;
vtkstream<<"DIMENSIONS 6 4 1"<<std::endl;
vtkstream<<"POINTS 24 double"<<std::endl;
for (unsigned int i = 0; i < 72; ++i) {
vtkstream<<myarray[i]<<" ";
}
} else {
vtkstream<<"BINARY"<<"\n";
vtkstream.close();
vtkstream.clear();
vtkstream.open("test01.vtk", std::ios::out | std::ios::app | std::ios::binary);
vtkstream<<"DATASET STRUCTURED_GRID"<<std::endl;
vtkstream<<"DIMENSIONS 6 4 1"<<std::endl;
vtkstream<<"POINTS 24 double"<<std::endl;
for (unsigned int i = 0; i < 72; ++i) {
vtkstream<<myarray[i];
}
}
vtkstream.close();
} else {
std::cout<<"ERROR"<<std::endl;
}
return 0;
}

ASCII 文件格式完美运行,但二进制版本在 ParaView 中产生以下错误:

Generic Warning: In ........\src\VTK\IO\vtkDataReader.cxx, line 1363 Error reading binary data!

我的VTK格式错在哪里?

最佳答案

似乎 VTK 假设二进制文件被写为大端,而大多数 PC 使用小端存储(参见 VTK file formats document 第 2 页的底部)。您能否尝试在写入二进制数据时交换字节顺序,看看是否能解决您的问题?

另请参阅 this VTK users post ,它与此问题类似。

关于c++ - 写入二进制 VTK 文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10913666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com