在写入二进制文件时,“std::ofstream::write”有时会写入超出应有字节数的字节
When writing into a binary file, `std::ofstream::write` sometimes writes more bytes than it should
我正在尝试将三维几何体写入二进制STL文件。以下是主程序的工作原理:
ParseSTL stl();
//generate the 3D model
std::string outfname = "test.stl";
std::ofstream outf(outfname, std::ios_base::out & std::ios_base::binary);
stl.writeBinarySTL(outf);
STL编写器为:
void writeBinarySTL(std::ofstream &outf)
{
char attr[2] = { 0 };
char header[80] = { 0 };
int count = 0;
outf.write(header, 80);
//count += 80;
//std::cout << "after header: file position: " << outf.tellp() << " bytes written: " << count << "n";
//if ((int)outf.tellp() != count)
// std::cout << "something is wrong!n";
outf.write(reinterpret_cast<char *>(&facecount), sizeof(int));
//count += sizeof(int);
//std::cout << "after number of faces: file position: " << outf.tellp() << " bytes written: " << count << "n";
//if ((int)outf.tellp() != count)
// std::cout << "something is wrong!n";
for (int i = 0; i < facecount; i++)
{
struct face{
float n[3];
float v[3][3];
} f;
for (int j = 0; j < 3; ++j)
f.n[j] = (float) normals[i][j];
for (int j = 0; j < 3; ++j)
for (int k = 0; k < 3; ++k)
f.v[j][k] = (float)vertices[faces[i][j]][k];
outf.write(reinterpret_cast<char *>(&f), sizeof(f));
//count += sizeof(f);
//std::cout << "after struct: file position: " << outf.tellp() << " bytes written: " << count << "n";
//if ((int)outf.tellp() != count)
//{
// std::cout << "something is wrong!n";
// break;
//}
outf.write(attr, 2);
//count += 2;
//std::cout << "after attributes: file position: " << outf.tellp() << " bytes written: " << count << "n";
//if ((int)outf.tellp() != count)
//{
// std::cout << "something is wrong!n";
// break;
//}
}
//std::cout << "Bytes written: " << count << "n";
}
注释的部分仅用于调试,facecount
是存储三角形数量的类ParseSTL
的成员,normals
是存储所有面的法线的3D矢量的矢量,vertices
是存储所有顶点的坐标的3D矢量矢量的矢量;faces
是存储属于三角形的vrtice的索引的3个整数的矢量。现在,如果我运行这个程序,这里是我得到的输出:
Vertex count = 4243
Face count = 3168
Edge count = 0
after header: file position: 80 bytes written: 80
after number of faces: file position: 84 bytes written: 84
after struct: file position: 132 bytes written: 132
after attributes: file position: 134 bytes written: 134
...
after struct: file position: 532 bytes written: 532
after attributes: file position: 534 bytes written: 534
after struct: file position: 583 bytes written: 582
something is wrong!
因此,行outf.write(reinterpret_cast<char *>(&f), sizeof(f));
似乎比它应该写入的多写入了1个字节。这种漂移偶尔会发生一次,最后,我得到的不是158,484
字节的文件,而是158,896
字节的文件。如果在写入结构后添加outf.seekp(count)
,那么最终的文件大小是正确的,但有些浮点值没有正确写入(因此漂移并不总是在结构的末尾)。
我想知道我做错了什么,导致这个额外的字节被写入文件。哦,我正在使用Microsoft Visual Stusio Express 2013。
此行
std::ofstream outf(outfname, std::ios_base::out & std::ios_base::binary);
需要
std::ofstream outf(outfname, std::ios_base::out | std::ios_base::binary);
^^^^^ The difference
可以通过省略std::ios_base::out
标志来简化它(感谢@Blastfurt的提示)。它还减少了此类错误悄悄出现的机会
std::ofstream outf(outfname, std::ios_base::binary);
相关文章:
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 基于字节数组生成静态范围整数值
- 将字节数组转换为带有字节序问题的指针
- 找到一种有效的方法,在 2 个巨大的缓冲区上执行 MAX,每字节字节
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- 字节数组初始化会导致 DirectX 崩溃
- 使用 swig 追加到字节数组
- C++ 替换字节数组中项的顺序
- QDataStream 读取和写入的字节数比 QFile::length() 报告要多
- 如何从保存在 Java 中C++的字节数组中读取数字?
- 如何通过libpq c api插入字节的数组
- i2c中的字节指针数组
- 以 C++ 为单位递增 16 字节字符数组
- new[] 一个包含构造函数的字节对象数组没有错,对吧?
- 创建未知长度的字节/字符数组
- 我如何将bitset转换为字节/uint8数组
- C++ <--> C# 修改字节编组数组
- 将m个字节的数组拆分为n个字节的块
- c++到c#:有3个元素的字节指针数组等于什么?