Hello Everybody,
I am creating and writing a binary file, and am curious about a particular behavior. I am writing a struct data to the file, and the unitialized fields always come up as 0xCC in the binary file.
#pragma pack(push, 1)
struct ExampleStruct
{
BYTE flag;
short index;
short code;
};
#pragma pop()
...Some class definitions
class StuctTest : CFile
....
StuctTest::StuctTest(const CString &csFilename)
{
CFileException cFileException;
m_FileStatus = 0;
int RetryCount = 0;
m_Filename = csFilename; // save the filename for the print function
BOOL bStat = Open(csFilename, modeReadWrite | modeCreate | modeNoTruncate |
shareDenyNone | typeBinary, &cFileException);
}
int StuctTest::WriteData(int loc, const ExampleStruct &exampleStruct)
{
long lOffset, lActual;
int Status = 0;
ExampleStruct example;
int sizeOfStruct = sizeof(struct ExampleStruct);
lOffset = (loc - 1) * sizeof(struct ExampleStruct);
// put the data read into the correct structure to write to the file
example.code = exampleStruct.code;
example.flag = exampleStruct.flag;
example.index = exampleStruct.index;
TRY
{
lActual = Seek(lOffset, CFile::begin);
Write((void *)&example, sizeof(struct ExampleStruct));
return 0;
}
CATCH(CFileException, pFileException)
{
}
END_CATCH
return Status;
}
Here is where the file gets created...
StuctTest test(_T("C:\\temp\\temp.dat"));
for (int i = 0; i < 3; i++)
{
ExampleStruct example;
example.flag = 0x20 * i + 1;
//example.index = i;
example.code = 0x1 * i;
test.WriteData(i + 1, example);
}
test.~StuctTest();
Here is the output:
Since the one field was commented out, is 0xCC typical behavior for an unitialized struct varaible?
Thanks!