I am using MS Visual C++ 7.1.3088 on Windows XP 5.1
I have two defined structures, struct1 and struct2 and would like to
write them at the beginning of a file. I use the following approach.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <windows.h>
#include <io.h>
int func(char *csFileName, struct1 s1Struct, struct2 s2Struct)
{
int iOutputFile;
int iBytes2Write, iBytesWritten;
if ((iOutputFile = _open(csFileName, _O_WRONLY | _O_BINARY | _O_APPEND
| _O_CREAT, _S_IWRITE)) == -1) /* Error handling*/
iBytes2Write = sizeof(struct1); // Size is 40 bytes
if ((iBytesWritten = _write(iOutputFile, (void *)&s1Struct,
iBytes2Write)) < iBytes2Write)
{
_close(iOutputFile);
// Error handling
}
iBytes2Write = sizeof(struct2); // Size is 104 bytes
if ((iBytesWritten = _write(iOutputFile, (void *)&s2Struct,
iBytes2Write)) < iBytes2Write)
{
_close(iOutputFile);
// Error handling
}
_close(iOutputFile);
}
For some reason, the first 8 bytes of the second structure are left
out. I can tell this by doing an octal dump on Unix. It is quite
clear. The last byte of s1Struct is immediately followed by the ninth
byte of s2Struct. I could just add an extra 8 bytes before the second
structure but would prefer a tidier solution.
Any assistance would be greatly appreciated.
Many thanks in advance,
Peter.