• Advertisement
Sign in to follow this  

ofstream::write writing more than _count bytes (resolved)

This topic is 4710 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing an export plugin for 3ds max, and I'm having a huge problem using ofstream::write. Sometimes (generally as the number of floats gets larger, this problem gets worse) when I call write(whatever, someNumber) and check the position before and after, the actual difference is greater than someNumber. If this happens even once and only 1 byte extra is written, my entire model is screwed. I made sure my plugin is thread-safe, and aside from that I'm at a loss as to why this might be happening, anyone have any ideas? [Edited by - digitec devil on March 30, 2005 8:38:22 PM]

Share this post


Link to post
Share on other sites
Advertisement

ofstream file(name);

IGameMesh* mesh = (IGameMesh*) currentObject;
mesh->SetCreateOptimizedNormalList();
mesh->InitializeData();
int numFaces = 0;
offby = 0;


numFaces = mesh->GetNumberOfFaces();

file.write((char*)&numFaces, sizeof(int));
float *glVertices = new float[sizeof(float)*numFaces*9];
float *glNormals = new float[sizeof(float)*numFaces*9];

// Vertices
for(int i = 0; i < numFaces; i++)
{
FaceEx * face = mesh->GetFace(i);
Point3 vertex1 = mesh->GetVertex(face->vert[0], true);
Point3 vertex2 = mesh->GetVertex(face->vert[1], true);
Point3 vertex3 = mesh->GetVertex(face->vert[2], true);

glVertices[i*9 ] = vertex1.x;
glVertices[i*9 + 1] = vertex1.y;
glVertices[i*9 + 2] = vertex1.z;
glVertices[i*9 + 3] = vertex2.x;
glVertices[i*9 + 4] = vertex2.y;
glVertices[i*9 + 5] = vertex2.z;
glVertices[i*9 + 6] = vertex3.x;
glVertices[i*9 + 7] = vertex3.y;
glVertices[i*9 + 8] = vertex3.z;
}

file.write((char*)glVertices, numFaces*36);

// Normals
for(int i = 0; i < numFaces; i++)
{
FaceEx * face = mesh->GetFace(i);
Point3 vertex1 = mesh->GetNormal(face->norm[0]);
Point3 vertex2 = mesh->GetNormal(face->norm[1]);
Point3 vertex3 = mesh->GetNormal(face->norm[2]);

glNormals[i*9 ] = vertex1.x;
glNormals[i*9 + 1] = vertex1.y;
glNormals[i*9 + 2] = vertex1.z;
glNormals[i*9 + 3] = vertex2.x;
glNormals[i*9 + 4] = vertex2.y;
glNormals[i*9 + 5] = vertex2.z;
glNormals[i*9 + 6] = vertex3.x;
glNormals[i*9 + 7] = vertex3.y;
glNormals[i*9 + 8] = vertex3.z;
}

file.write((char*)glNormals, numFaces*36);

file.close();

Share this post


Link to post
Share on other sites
[EDIT: Beat]

"ofstream file(name);" is opening the file in text mode, so that may be your problem. Try changing that to: "ofstream file(name, ios::binary );" and see if it changes anything.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement