For Programmers: Free Programming Magazines  


Home > Archive > VC Language > November 2005 > _write Leaving Out Bytes









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author _write Leaving Out Bytes
MajorSetback@excite.com

2005-11-25, 7:02 pm

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.

Jack Klein

2005-11-25, 7:02 pm

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.

rossum

2005-11-25, 7:02 pm

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
Frank Hickman [MVP]

2005-11-26, 7:01 pm

"rossum" <rossum48@coldmail.com> wrote in message
news:5qbfo15dg9674p67b97bf45nnb77e28c7m@
4ax.com...
> On 25 Nov 2005 13:37:57 -0800, MajorSetback@excite.com wrote:
>
> <stdio.h> is deprecated, prefer <stdio>
> <stdlib.h> is deprecated, prefer <<stdlib>
> 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.


> Are you sure you want "=" rather then "==" here?


Quite sure, are you following the parenthesis?


> Check this also:
> assert(iBytes2Write == 104);
> Same comment on = and ==
>
> The declarations of the structures would help.


Agreed.


>
> rossum
>
>
>
> The ultimate truth is that there is no ultimate truth


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


wittempj@hotmail.com

2005-11-26, 7:01 pm

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

Tim Roberts

2005-11-27, 3:58 am

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

2005-11-28, 7:07 pm

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.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com