Hi, all. I'm having some trouble with data being written in binary file i/o. Here's the gist of what I'm doing:
I have an array (MyMap[3]) of a struct that has one int and one long. I want to record the values of the structs into a file.
Simple enough, right? Except that the last set of data in my array records "1" for the int data, yet records the proper value of the long.
Here's the relevant code:
Code:
//declared at the start of the file (after the headers, etc.)
struct Map {
int MyInt;
long MyLong;
};
Map MyMap[3];
int iMapCntr=0
//triggered when the left mouse button is clicked:
//Writes struct array data to file sequentially(0 to 3)
ULONG file_ptr;
file_ptr=0;
HANDLE hFile=CreateFile("test.dat",GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
for(iMapCntr=0;iMapCntr<4;iMapCntr++)
{
file_ptr=iMapCntr*sizeof(Map);
WriteFile(hFile,&MyMap[iMapCntr],sizeof(Map),&file_ptr,NULL);
}
CloseHandle(hFile);
//triggered when the right mouse button is clicked:
//Reads struct array data in from file sequentially(0 to 3)
ULONG file_ptr;
file_ptr=0;
HANDLE hFile=CreateFile("test.dat",GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
for(iMapCntr=0;iMapCntr<4;iMapCntr++)
{
file_ptr=iMapCntr*sizeof(Map);
ReadFile(hFile,&MyMap[iMapCntr],sizeof(Map),&file_ptr,NULL);
//for debugging puposes
char cInt[20], cLong[20], cSep[5]=" , ";
_itoa(MyMap[iMapCntr].MyInt,cInt,10);
_itoa(MyMap[iMapCntr].MyLong,cLong,10);
strcat(cInt,cSep);
strcat(cInt,cLong);
MessageBox(hWndMain,cInt,"Test",NULL);
//end debugging code
}
CloseHandle(hFile);
Everything returns properly except on the last call MyMap[3].MyInt returns 1.
The values are set with keystrokes not listed above. I've even tried hardcoding the MyInt value and it still returns 1.
Am I incorrectly setting the file_ptr variable?
Thanks in advance for any help you can give.
Jason