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.

Re: _write Leaving Out Bytes by Jack

Jack
Fri Nov 25 16:14:25 CST 2005

On 25 Nov 2005 13:37:57 -0800, MajorSetback@excite.com wrote in
comp.os.msdos.programmer:

> Re: _write Leaving Out Bytes

There is no _write function in either the C or C++ language, it is a
non-standard extension. That makes it off-topic of
alt.comp.lang.learn.c-c++.

> I am using MS Visual C++ 7.1.3088 on Windows XP 5.1

Then your question has nothing at all to do with MS-DOS, making it
off-topic in comp.os.msdos.programmer.

[snip]

> Any assistance would be greatly appreciated.

How about a suggestion that you stop indiscriminately cross posting to
newsgroups where your question is off-topic.

> Many thanks in advance,

You're welcome. Followups trimmed.

> Peter.

Re: _write Leaving Out Bytes by rossum

rossum
Fri Nov 25 18:46:09 CST 2005

On 25 Nov 2005 13:37:57 -0800, MajorSetback@excite.com wrote:

>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>
<stdio.h> is deprecated, prefer <stdio>
>#include <stdlib.h>
<stdlib.h> is deprecated, prefer <<stdlib>
>#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 the size is 40 bytes then check it:
assert(iBytes2Write == 40);
>if ((iBytesWritten = _write(iOutputFile, (void *)&s1Struct,
Are you sure you want "=" rather then "==" here?
>iBytes2Write)) < iBytes2Write)
> {
> _close(iOutputFile);
> // Error handling
> }
>
>iBytes2Write = sizeof(struct2); // Size is 104 bytes
Check this also:
assert(iBytes2Write == 104);
>if ((iBytesWritten = _write(iOutputFile, (void *)&s2Struct,
Same comment on = and ==
>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.

The declarations of the structures would help.

rossum

>
>Many thanks in advance,
>Peter.


The ultimate truth is that there is no ultimate truth

Re: _write Leaving Out Bytes by Frank

Frank
Sat Nov 26 14:40:00 CST 2005

"rossum" <rossum48@coldmail.com> wrote in message
news:5qbfo15dg9674p67b97bf45nnb77e28c7m@4ax.com...
> On 25 Nov 2005 13:37:57 -0800, MajorSetback@excite.com wrote:
>
>>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>
> <stdio.h> is deprecated, prefer <stdio>
>>#include <stdlib.h>
> <stdlib.h> is deprecated, prefer <<stdlib>
>>#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 the size is 40 bytes then check it:
> assert(iBytes2Write == 40);

IMHO, this is really a "6 in one, half dozen in the other" type of situation
because this assignment is basically the same as using sizeof() as the
actual parameter to the function.


>>if ((iBytesWritten = _write(iOutputFile, (void *)&s1Struct,
> Are you sure you want "=" rather then "==" here?

Quite sure, are you following the parenthesis?


>>iBytes2Write)) < iBytes2Write)
>> {
>> _close(iOutputFile);
>> // Error handling
>> }
>>
>>iBytes2Write = sizeof(struct2); // Size is 104 bytes
> Check this also:
> assert(iBytes2Write == 104);
>>if ((iBytesWritten = _write(iOutputFile, (void *)&s2Struct,
> Same comment on = and ==
>>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.
>
> The declarations of the structures would help.

Agreed.


>
> rossum
>
>>
>>Many thanks in advance,
>>Peter.
>
>
> The ultimate truth is that there is no ultimate truth

--
============
Frank Hickman
Microsoft MVP
NobleSoft, Inc.
============
Replace the _nosp@m_ with @ to reply.



Re: _write Leaving Out Bytes by wittempj

wittempj
Sat Nov 26 17:28:15 CST 2005

Try groups like comp.os.ms-windows.programmer.misc or
comp.os.ms-windows.programmer.tools.* as your question is the Windows
platform specific.


Re: _write Leaving Out Bytes by Tim

Tim
Sat Nov 26 22:51:32 CST 2005

MajorSetback@excite.com wrote:
>
>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.

Show us the definitions of struct1 and struct2. My guess is you have some
weirdness in structure packing that is causing you unexpected results. If
we have a full, runnable source, we can check it out.

>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.

How did you get the file to Unix for the dump? Was the file in Windows 144
bytes long?
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Re: _write Leaving Out Bytes by MajorSetback

MajorSetback
Mon Nov 28 11:20:45 CST 2005

Hi Rossum, Frank and Tim,

I found the problem. It was the _O_APPEND switch (carelessness on my
part). I had actually gotten it working but the correct o/p was
appended to the end of the bad o/p.

Thanks very much for your help,
Peter.