写入二进制文件时,`std::ofstream::write` 有时会写入比应有的更多字节
When writing into a binary file, `std::ofstream::write` sometimes writes more bytes than it should
我正在尝试将 3D 几何体写入二进制 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
是class的成员,ParseSTL
存储三角形数,normals
是3D向量向量存储所有面的法线,vertices
是存储所有顶点坐标的 3D 向量向量,faces
是存储属于三角形的顶点索引的 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
字节的文件。如果我在写完struct后加上一个outf.seekp(count)
,最终的文件大小是正确的,但是有些float没有写正确(所以漂移并不总是在struct的末尾)。
我想知道我做错了什么导致将这些额外的字节写入文件。哦,我正在使用 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
标志来简化(感谢@Blastfurnace 的提示)。它还减少了出现此类错误的可能性。
std::ofstream outf(outfname, std::ios_base::binary);
我正在尝试将 3D 几何体写入二进制 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
是class的成员,ParseSTL
存储三角形数,normals
是3D向量向量存储所有面的法线,vertices
是存储所有顶点坐标的 3D 向量向量,faces
是存储属于三角形的顶点索引的 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
字节的文件。如果我在写完struct后加上一个outf.seekp(count)
,最终的文件大小是正确的,但是有些float没有写正确(所以漂移并不总是在struct的末尾)。
我想知道我做错了什么导致将这些额外的字节写入文件。哦,我正在使用 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
标志来简化(感谢@Blastfurnace 的提示)。它还减少了出现此类错误的可能性。
std::ofstream outf(outfname, std::ios_base::binary);