I'm working on a small program to test some file i/o. I have a class (CMything):
Pretty simple and straight forward. Just a generic object that I can use in my file read/write ops. I have an array of 5 of these objects. I have 4 functions: 1 to fill the objects with data, 1 to write the data to a file, 1 to read the data in from a file and 1 to display the data read in so I can make sure everything went ok.Code:class CMything
{
private:
long l1;
long l2;
long l3;
RECT rc;
char cC[30];
public:
CMything();
void Setl1(long lNew);
long Getl1();
void Setl2(long lNew);
long Getl2();
void Setl3(long lNew);
long Getl3();
void SetRC(long lL, long lT, long lR, long lB);
LPRECT GetRC();
void SetCC(char cNew[30]);
char* GetCC();
~CMything();
};
Everything works fine until I run DisplayData(). It goes through the for loop fine until it get to (lCount==4) and then I get an invalid page fault error.Code:CMything cmThing[5];
void FillData()
{
for(lCount=0;lCount<5;lCount++)
{
cmThing[lCount].SetCC("Jeff Lenoce");
cmThing[lCount].Setl1(lCount*2);
cmThing[lCount].Setl2(cmThing[lCount].Getl1()+1);
cmThing[lCount].Setl3(cmThing[lCount].Getl2()+1);
cmThing[lCount].SetRC(0,0,32,32);
}
}
void SaveData()
{
lSize=sizeof(CMything);
ULONG fp=0;
HANDLE hFile=CreateFile("cmThings.dat",GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
for(lCount=0;lCount<5;lCount++)
{
fp=lCount*lSize;
WriteFile(hFile,&cmThing[lCount],lSize,&fp,NULL);
}
CloseHandle(hFile);
}
void ReadData()
{
lSize=sizeof(CMything);
ULONG fp=0;
HANDLE hFile=CreateFile("cmThings.dat",GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
for(lCount=0;lCount<5;lCount++)
{
fp=lCount*lSize;
ReadFile(hFile,&cmThing[lCount],lSize,&fp,NULL);
}
CloseHandle(hFile);
}
void DisplayData()
{
char cL1[20], cL2[20], cL3[20], cCount[20];
char cRL[20], cRT[20], cRR[20], cRB[20];
char cSep[5]=" - ";
RECT rcTest;
for(lCount=0;lCount<5;lCount++)
{
char cCat[10]="My Thing";
_ltoa(cmThing[lCount].Getl1(),cL1,10);
_ltoa(cmThing[lCount].Getl2(),cL2,10);
_ltoa(cmThing[lCount].Getl3(),cL3,10);
CopyRect(&rcTest,cmThing[lCount].GetRC());
_ltoa(rcTest.left,cRL,10);
_ltoa(rcTest.top,cRT,10);
_ltoa(rcTest.right,cRR,10);
_ltoa(rcTest.bottom,cRB,10);
_ltoa(lCount,cCount,10);
strcat(cCat," = ");
strcat(cCat,cCount);
strcat(cCat,"\n");
strcat(cCat,"Longs:");
strcat(cCat,"\n");
strcat(cCat,cL1);
strcat(cCat,"\n");
strcat(cCat,cL2);
strcat(cCat,"\n");
strcat(cCat,cL3);
strcat(cCat,"\n");
strcat(cCat,"RECTs");
strcat(cCat,"\n");
strcat(cCat,cRL);
strcat(cCat,"\n");
strcat(cCat,cRT);
strcat(cCat,"\n");
strcat(cCat,cRR);
strcat(cCat,"\n");
strcat(cCat,cRB);
MessageBox(NULL,cCat,"Debug",NULL);
}
}
This doesn't happen if I don't populate the variables with either FillData() or ReadData(). It runs fine and just gives me a MessageBox with a bunch of 0's for my values.
My mistake is most likely then in my FillData() function, but I just don't see it.