Hi everyone.
I have what seemed to be a simple problem at the start (famous last words) but now I am pretty confused as to whats going wrong.
I need to write several structures to a binary file. Within these structures are more, smaller structures, and within those are the actual data types (unsigned shorts, unsigned chars) that I want to keep track of. The following are the 'base' structures':
Now, when I try and write to file all of the structures, according to my math, I should have a file size near 550 bytes. However when I am done writing the file and go to check, the size is about 710. Big whoop you might say, but it is a requirement for this project that I get the file size to under 512 bytes, and it ought to be much closer than 710 right now. Keep in mind that I am only testing the application and do not have much, if any, manipulations to these data types and are being written as their default values of 0. So in trying to figure out what I am doing wrong, I wrote only one of the larger structures that has a collection of 3 sub structures, each with 5 ushort values:Code:struct Time { unsigned short hour; unsigned char minute; }; struct LogVersion { unsigned char digit1; unsigned char digit2; }; struct Date { unsigned char month; unsigned char day; unsigned char year; }; struct Counter { unsigned short count; }; struct BatterySlope { unsigned char AverageSlope; unsigned short ComputationCount; }; struct PowerParams { unsigned char AverageSlope; unsigned char AverageOffset; }; struct AverageValue { unsigned char Average; };
This ought to yield a file size of 30 bytes, and it does. I move onto the next one, which is larger:Code:struct mlog { struct Counter Count1[5]; struct Counter Count2[5]; struct Counter Count3[5]; };
Code:struct glog { struct LogVersion Version; struct Date CurrentDate; struct Counter BootCount[6]; struct Counter LockCount[5]; struct Counter ProgramCount[5]; struct Counter StandbyCount[6]; struct Counter RCVolumeCount; struct Counter RCSensitivityCount; struct Counter RCProgramCount; struct Counter RCZoomControlCount; struct Counter DuoPhoneCount; struct Counter ListeningCheckCount; struct Counter iComBTPhoneCount; struct Counter iComA2DPCount; struct Counter iComJackCount; struct Counter iComFMCount; struct Counter ErrorCount; struct Time ZoomCtlUseTime; struct Time DuoPhoneUseTime; struct Time iComStreamingUseTime; };
Did a little counting, and based on what I have here, the file size ought to be 80 bytes, based on having 80 bytes worth of data scattered in this structure. I check the file size after write, and it is 88 bytes??? I have gone through and written the items individually to a file as well, and got the same result. I also have written just one item to file to be absolutely sure htat my items are the right size. For some reason if I try and write, for example, CurrentVersion.digit1, and then CurrentVersion.digit2 to file by themselves, the file size is 4 bytes when I have only written two 1-byte chars??? I assumed that this would bypass the whole 'structure padding' problem that I have read about as far as adding extra bytes to structures and putting those into a write, but it seems to have not done so if this ends up being the problem, and this is the point where I am seriously questioning my understanding of everything I am working with and its not just 'woops, I put in a short where I shoulda put in a char.'
I have a third structure that I won't list because its already getting a little wordier than I wanted, but this structure is even larger than 'glog', has more of this mystery error in file size, and to top it off I have an array of 5 of those high size structures, which creates the gobs of extra file space that I sorely need to remove. Can someone tell me if theres some kind of fundamental understanding I am missing between structures, writing them to files, or something else I've overlooked?