So finally after a little waking up i fixed my write to a file in raw binary problem. Now I am trying to load the same data from the file i created. Here is the code I came up with.
#include <fstream>
#include ".\vertexs.h"
using namespace std;
int LoadVerts(char *s_fName, OURCUSTOMVERTEX *p_Verts, short *p_Indices)
{
long i_CountVerts;
long i_CountInds;
unsigned char buffer[sizeof(OURCUSTOMVERTEX)];
basic_ifstream<unsigned char> f_DataFile;
f_DataFile.open(s_fName, std::ios_base::in | std::ios_base::binary);
f_DataFile.get(buffer, sizeof(unsigned long));
memcpy_s(&i_CountVerts, sizeof(unsigned long), &buffer, sizeof(unsigned long));
f_DataFile.get(buffer, sizeof(unsigned long));
memcpy_s(&i_CountInds, sizeof(unsigned long), &buffer, sizeof(unsigned long));
OURCUSTOMVERTEX *p_temp_Verts = new OURCUSTOMVERTEX[i_CountVerts];
short *p_temp_Indices = new short[i_CountInds];
for (int temp = 0; temp < i_CountVerts; temp++)
{
f_DataFile.get(buffer, sizeof(OURCUSTOMVERTEX));
memcpy(&p_temp_Verts[temp], &buffer, sizeof(OURCUSTOMVERTEX));
}
for (int temp2 = 0; temp2 < i_CountInds; temp2++)
{
f_DataFile.get(buffer, sizeof(short));
memcpy(&p_Indices[temp2], &buffer, sizeof(short));
}
f_DataFile.close();
delete [] p_Verts;
p_Verts = p_temp_Verts;
delete [] p_Indices;
p_Indices = p_temp_Indices;
return 0;
}
The struct OURCUSTOMVERTEX is defined in the vertexs.h header file and contains 3 floats (x, y, z) and a unsigned long (color). When I build it no errors are returned at all, not even so much as a warning. But when I run this a system error as follows occurs:
Unhandled exception at 0x7c9377a2 in saveverts.exe: 0xC0000005: Access violation writing location 0x00030ffc.
Through a little breaking i found that the problem is coming in at the following portion of code:
for (int temp2 = 0; temp2 < i_CountInds; temp2++)
{
f_DataFile.get(buffer, sizeof(short));
memcpy(&p_Indices[temp2], &buffer, sizeof(short));
}
After stepping through the function, I found that the problem is originating here:
f_DataFile.get(buffer, sizeof(long));
memcpy_s(&i_CountInds, sizeof(long), &buffer, sizeof(long));
At this point with my sample run the buffer should read 09 00 00 00 ... as the long stored in the file by the save routine stores an unsigned long with value 9, which is what is being read here. Instead the buffer reads 00 09 00 00, which is 900 hex, which is 2304 decimal. here inlays the problem, i believe. In any event, from my perspective this is the root of the problem, and unfortunately I am unable to troubleshoot it further on my own and must ask for help.
Following is the SaveVerts routine, in hopes that it will help:
int SaveVerts(char *s_fName, OURCUSTOMVERTEX *p_Verts, short *p_Indices, long i_CountVerts, long i_CountInds)
{
unsigned char buffer[sizeof(OURCUSTOMVERTEX)];
basic_ofstream<unsigned char> f_DataFile;
f_DataFile.open(s_fName, std::ios_base::out | std::ios_base::binary);
memcpy(&buffer, &i_CountVerts, sizeof(unsigned long));
f_DataFile.write(buffer, sizeof(unsigned long));
memcpy(&buffer, &i_CountInds, sizeof(unsigned long));
f_DataFile.write(buffer, sizeof(unsigned long));
for (int temp = 0; temp < i_CountVerts; temp++)
{
memcpy(&buffer, &p_Verts[temp], sizeof(OURCUSTOMVERTEX));
f_DataFile.write(buffer, sizeof(OURCUSTOMVERTEX));
}
for (int temp2 = 0; temp2 < i_CountInds; temp2++)
{
memcpy(&buffer, &p_Indices[temp2], sizeof(short));
f_DataFile.write(buffer, sizeof(short));
}
f_DataFile.close();
return 0;
}
And finally this is the section of code in assembly (for all the ASM gurus out there) once the program crash and debug pops:
7C93779C mov edi,edi
7C93779E push ebp
7C93779F mov ebp,esp
7C9377A1 push ecx
7C9377A2 push ecx <**************problem line
7C9377A3 push edi
7C9377A4 mov edi,7C97C320h
7C9377A9 cmp dword ptr ds:[7C97C320h],edi
7C9377AF jne 7C942DA0
7C9377B5 xor al,al
7C9377B7 pop edi
7C9377B8 leave
7C9377B9 ret 8
Thanks in advance for any insights.