[英]C++/Qt : Invalid STL file - could not read first 6 bytes
std::string fname = "cube.stl";
FILE *inf = std::fopen(fname.c_str(), "rb");
if (inf == NULL) {
//throw std::runtime_error(std::string("Cannot open file ") + fname);
qglColor(Qt::black);
this->renderText(10, 10, "Cannot open file ");
}
char buffer[6];
size_t bytes_read = fread(buffer, 1, 6, inf);//copy file to buffer
QString bytes_read_diaplay = QString::number(bytes_read);
qglColor(Qt::black);
this->renderText(10, 300, bytes_read_diaplay);
if (bytes_read != 6) {
qglColor(Qt::black);
this->renderText(10, 10, "Invalid STL file - could not read first 6 bytes.");
}
std::rewind(inf);
if (0 == std::memcmp(buffer, "solid ", 6)) {
//read_ascii_file(inf);
qglColor(Qt::black);
this->renderText(10, 30, "read_ascii_file");
} else {
//read_binary_file(inf);
qglColor(Qt::black);
this->renderText(10, 30, "read_binary_file");
char buffer[80];
size_t num_read = fread(buffer, 1, 80, inf);
if (num_read != 80) {
qglColor(Qt::black);
this->renderText(10, 50, "Invalid binary STL file - could not read 80 byte header.");
}
std::memcpy(header, buffer, 80);
unsigned int num_tris = 0;
num_read = std::fread(&num_tris, sizeof(unsigned int), 1, inf);
if (num_read != 1) {
qglColor(Qt::black);
this->renderText(10, 70, "Invalid binary STL file - could not read number of triangles from binary STL file.");
}
}
std::fclose(inf);
我得到bytes_read是4,所以我得到了:
无效的STL文件-无法读取前6个字节。
read_binary_file
无效的二进制STL文件-无法读取80字节的标头。
无效的二进制STL文件-无法从二进制STL文件读取三角形数量。
“ cube.stl”是.stl文件,但是为什么会显示这样的文件? 我哪里错了? 如何在cube.stl中获得顶点坐标? 我的.stl文件是: http : //www.mediafire.com/download/hnnw444anih201n/cube.stl
经过检查和检查并检查。 终于,我找到了方向。 D ***,错误很简单。 我更改了std :: string fname =“ cube.stl”的路径; (将cube.stl转换为D:/.../ cube.stl)。 太奇妙了。 跑。
您似乎对Ascii和Binary文件之间的区别有些困惑。
fopen
的“ rb”标志将作为二进制文件打开。 cube.stl是二进制文件吗? .stl文件可以在Binary或Ascii中。
此外,您如何验证文件大小? 您期望输入6个char
或80个字节的二进制。 这些数字从哪里来?
简而言之,您的问题是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.