I am at a crossroads here
What my objective is
I've written my own dynamic array. I have a structure named 'header' like so:
struct header
{
int numBlocks;
int numBlockTypes;
GrowableArray<int> blockorder;
GrowableArray<string> blockTypes;
};
So I have a few ints and a few GrowableArray, one of int and one of string.
I want to create a copy of header called 'head' and assign values. Then I want to write the structure to a binary file. Now, once these values have been written, I want to read them again, but not from memory, meaning I close the program then open it again, so the memory addresses are not the same.
All goes well for the two int variables, but not the arrays. I sort of know why, but I can't bring my head around it to find a solution. Any help?
main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
#include <array>
#include "gGrowableArray.h"
using namespace std;
struct header
{
int numBlocks;
int numBlockTypes;
GrowableArray<int> blockorder;
GrowableArray<string> blockTypes;
};
struct vertex
{
float x,y,z;
};
struct triangle
{
int v1,v2,v3;
};
class MeshReaderAlpha
{
public:
MeshReaderAlpha(){ headerData = new header(); }
~MeshReaderAlpha(){}
HRESULT readFile(const char* fileName);
HRESULT readHeader(ifstream &in);
header* headerData;
GrowableArray<vertex> vertices;
GrowableArray<triangle> triangles;
int offset;
};
HRESULT MeshReaderAlpha::readHeader(ifstream &in)
{
return S_OK;
}
HRESULT MeshReaderAlpha::readFile(const char* fileName)
{
ifstream istream(fileName, ios::binary|ios::in);
//Read the header
headerData = new header();
istream.seekg(0);
istream.read((char*)headerData, sizeof(*headerData));
istream.seekg(sizeof(*headerData));
if( headerData->numBlocks < headerData->blockTypes.GetSize() || headerData->numBlocks < headerData->numBlockTypes)
{
cout << "Warning: NumBlocks is lower than BlockTypes count!" << endl;
}
cout << headerData->blockTypes[0] << endl;//Because of here
return S_OK;
}
int main() {
MeshReaderAlpha* mesh = new MeshReaderAlpha();
mesh->readFile("format.bin");//Error Happens here
header head;
head.numBlocks = 4;
head.numBlockTypes = 4;
head.blockorder.Add(0);
head.blockorder.Add(1);
head.blockorder.Add(2);
head.blockorder.Add(3);
head.blockTypes.Add("vertex");
head.blockTypes.Add("vertex");
head.blockTypes.Add("vertex");
head.blockTypes.Add("triangle");
vertex v1 = {0.0,0.0,0.0};
vertex v2 = {0.0,1.0,0.0};
vertex v3 = {1.0,0.0,0.0};
triangle t1 = {0,1,2};
ofstream stream("format.bin", ios::binary|ios::out);
stream.write((const char*)&head,sizeof(head));
stream.write((const char*)&head.blockTypes,sizeof(head));
stream.write((const char*)&v1, sizeof(v1));
stream.write((const char*)&v2, sizeof(v2));
stream.write((const char*)&v3, sizeof(v3));
stream.write((const char*)&t1, sizeof(t1));
cout << "Written to bin file!" << endl;
stream.close();
ifstream istream("format.bin", ios::binary|ios::in);
header* heading = new header();
istream.seekg(0);
istream.read((char*)heading, sizeof(*heading));
istream.seekg(sizeof(*heading));
if( heading->numBlocks < heading->blockTypes.GetSize() || heading->numBlocks < heading->numBlockTypes)
{
cout << "Warning: NumBlocks is lower than BlockTypes count!" << endl;
}
GrowableArray<vertex> verts;
GrowableArray<triangle> tris;
int offset = istream.tellg();
for( int i = 0; i < heading->numBlocks; i++ )
{
if( i == heading->blockorder[i] )
{
if(heading->blockTypes[i] == "vertex")
{
vertex* v = new vertex();
istream.read((char*)v, sizeof(*v));
verts.Add(*v);
istream.seekg(offset + sizeof(*v));
offset = istream.tellg();
}
else if(heading->blockTypes[i] == "triangle")
{
triangle* t = new triangle();
istream.read((char*)t, sizeof(*t));
tris.Add(*t);
istream.seekg(offset + sizeof(*t));
offset = istream.tellg();
}
}
}
for( int o = 0; o < verts.GetSize(); o++ )
{
cout << "Verticies: " << verts[o].x << "; " << verts[o].y << "; " << verts[o].z << "; " << endl;
}
for( int o = 0; o < tris.GetSize(); o++ )
{
cout << "Triangles: " << tris[o].v1 << "; " << tris[o].v2 << "; " << tris[o].v3 << "; " << endl;
}
system("pause");
return 0;
}
growableArray.h
#ifndef gArr_H
#define gArr_H
#include "stdafx.h"
/*
* A growable array that utalises a class
*/
template<typename TYPE> class GrowableArray
{
public:
GrowableArray()
{
aData = NULL; aSize = 0; aMaxSize = 0;
}
GrowableArray( const GrowableArray <TYPE>& a)
{
for( int i = 0; i < aMaxSize; i++ ) Add( a.aData[i] );
}
~GrowableArray()
{
}
const TYPE& operator[](int nIndex) const
{
return GetAt(nIndex);
}
TYPE& operator[](int nIndex)
{
return GetAt(nIndex);
}
GrowableArray& operator=( const GrowableArray <TYPE>& a )
{
if( this == &a ) return *this; for( int i = 0; i < a.aSize;
i++ ) Add( a.aData[i] ); return *this;
}
HRESULT SetSize( int newSize );
int inline GetSize(void){return aSize; }
HRESULT Add( const TYPE& value );
HRESULT Insert( int index, const TYPE& value );
HRESULT SetAt( int index, const TYPE& value );
TYPE& GetAt( int index ) const
{
assert( index >= 0 && index < aSize );
return aData[index];
}
TYPE* aData;
int aSize;
int aMaxSize;
HRESULT SetSizeInternal( int NewSize );
};
template<typename TYPE> HRESULT GrowableArray <TYPE>::SetSizeInternal( int NewSize )
{
if( NewSize < 0 || ( NewSize > INT_MAX / sizeof( TYPE ) ) )
{
assert( false );
return E_INVALIDARG;
}
if( NewSize == 0 )
{
// Shrink to 0 size & cleanup
if( aData )
{
free( aData );
aData = NULL;
}
aMaxSize = 0;
aSize = 0;
}
else if( aData == NULL || NewSize > aMaxSize )
{
// Grow array
int nGrowBy = (aMaxSize == 0 ) ? 16 : aMaxSize;
// Limit nGrowBy to keep m_nMaxSize less than INT_MAX
if( ( UINT )aMaxSize + ( UINT )nGrowBy > ( UINT )INT_MAX )
nGrowBy = INT_MAX - aMaxSize;
NewSize = __max( NewSize, aMaxSize + nGrowBy );
// Verify that (nNewMaxSize * sizeof(TYPE)) is not greater than UINT_MAX or the realloc will overrun
if( sizeof( TYPE ) > UINT_MAX / ( UINT )NewSize )
return E_INVALIDARG;
TYPE* pDataNew = ( TYPE* )realloc( aData, NewSize * sizeof( TYPE ) );
if( pDataNew == NULL )
return E_OUTOFMEMORY;
aData = pDataNew;
aMaxSize = NewSize;
}
return S_OK;
}
template<typename TYPE> HRESULT GrowableArray <TYPE>::Add( const TYPE& value )
{
HRESULT hr;
if( FAILED( hr = SetSizeInternal( aSize + 1 ) ) )
return hr;
assert( aData != NULL );
// Construct the new element
::new ( &aData[aSize] ) TYPE;
// Assign
aData[aSize] = value;
++aSize;
return S_OK;
}
#endif

New Topic/Question
Reply




MultiQuote






|