C++ writing int to binary file

Derek Davis derek.davis at gmail.com
Mon Apr 27 22:24:30 MDT 2009


On Mon, Apr 27, 2009 at 11:29 PM, Jessie Morris
<jessie at confettiantiques.com> wrote:
> On Monday 27 April 2009 9:23:13 pm Byron Clark wrote:
>> On Mon, Apr 27, 2009 at 08:52:20PM -0600, Jessie Morris wrote:
>> > file.write(reinterpret_cast<char *>(items),sizeof(int));
>>
>> Assuming that items is an int, you'll probably want &items instead of
>> items.
> That was EXACTLY what was wrong. But so that I understand what was going on,
> why is that needed?

file.write writes a block of memory pointed to by the first parameter.
&items gets the address of the int, so it passes in a pointer to its
value. Without it, write interprets the int as an address, and tries
to read an int from wherever it points.

Derek



More information about the PLUG mailing list