Name: Anonymous 2007-01-19 23:53
Teh Problem: I'm trying to read a binary 3d file format. The member variable types object_data, vertex, normal, and poly are structures that match with what is described in the file spec. I've looked at the hex directly, and it looks like the addresses line up, so I think I can pull off this read operation. But as soon as I try to use any of the dynamic arrays (by outputting to another file), i get a seg fault/access violation. I pray to the /prog/ gods for guidance!
Teh class:
class PolyObject
{
public:
PolyObject (); // blank constructor
PolyObject (ifstream& geoFile, int address); // read constructor
PolyObject (const PolyObject& source); // copy constructor
~PolyObject (); // destructor
void write_obj (ofstream& jout, int& lastV, int& lastVT);
// write
private:
object_data POtag; // has format per .geo file spec
char *pName; // name of polygon object
int lenName;
vertex *pVertexList; // vertex dynamic array
normal *pNormalList; // normal dynamic array
poly *pPolyList; // poly dynamic array
int totalNormals; // face normals + vertex normals
};
Teh Construxxor:
PolyObject::PolyObject (ifstream& geoFile, int address)
{
geoFile.seekg(address);
geoFile.read((char*)&POtag, SIZE_OBJECT_ENTRY);
totalNormals = POtag.nVertexNormals + POtag.nFaceNormals;
lenName = getStringLength(geoFile, POtag.oName);
pName = new char[lenName]; // should be null-terminated ok
pVertexList = new vertex[POtag.nVertices];
pNormalList = new normal[totalNormals];
pPolyList = new poly[POtag.nPolys];
geoFile.seekg(POtag.oName); // read in name
geoFile.read(pName, lenName);
geoFile.seekg(POtag.oVertexList); // read in vertices (however many)
geoFile.read((char*)pVertexList, POtag.nVertices * SIZE_VERTEX_ENTRY);
geoFile.seekg(POtag.oNormalList); // read in normals (however many)
geoFile.read((char*)pNormalList, totalNormals * SIZE_NORMAL_ENTRY);
geoFile.seekg(POtag.oPolyList); // read in polys
geoFile.read((char*)pPolyList, POtag.nPolys * SIZE_POLY_ENTRY);
}
Teh class:
class PolyObject
{
public:
PolyObject (); // blank constructor
PolyObject (ifstream& geoFile, int address); // read constructor
PolyObject (const PolyObject& source); // copy constructor
~PolyObject (); // destructor
void write_obj (ofstream& jout, int& lastV, int& lastVT);
// write
private:
object_data POtag; // has format per .geo file spec
char *pName; // name of polygon object
int lenName;
vertex *pVertexList; // vertex dynamic array
normal *pNormalList; // normal dynamic array
poly *pPolyList; // poly dynamic array
int totalNormals; // face normals + vertex normals
};
Teh Construxxor:
PolyObject::PolyObject (ifstream& geoFile, int address)
{
geoFile.seekg(address);
geoFile.read((char*)&POtag, SIZE_OBJECT_ENTRY);
totalNormals = POtag.nVertexNormals + POtag.nFaceNormals;
lenName = getStringLength(geoFile, POtag.oName);
pName = new char[lenName]; // should be null-terminated ok
pVertexList = new vertex[POtag.nVertices];
pNormalList = new normal[totalNormals];
pPolyList = new poly[POtag.nPolys];
geoFile.seekg(POtag.oName); // read in name
geoFile.read(pName, lenName);
geoFile.seekg(POtag.oVertexList); // read in vertices (however many)
geoFile.read((char*)pVertexList, POtag.nVertices * SIZE_VERTEX_ENTRY);
geoFile.seekg(POtag.oNormalList); // read in normals (however many)
geoFile.read((char*)pNormalList, totalNormals * SIZE_NORMAL_ENTRY);
geoFile.seekg(POtag.oPolyList); // read in polys
geoFile.read((char*)pPolyList, POtag.nPolys * SIZE_POLY_ENTRY);
}