![]() |
I need to read and write a linked list to a binary file so that it may be kept throughout the life of the program, but I'm having a hell of a time and my sourcecode doesn't work and I can't figure it out.
PLEASE HELP! main.cpp: :
#include "stdafx.h" |
CODE tags added.
|
>in.read((char*)(one),sizeof(person));
>out.write((char*)thisguy,sizeof(person)); This is bad on multiple levels. When working with raw binary I/O you should remember two things: read and write for non-trivial classes will fail, and read and write for structures that contain pointers will fail. The first problem stems from the difficulty of converting an object to a reasonable sequence of bytes. The second problem is caused by the indirect nature of pointers, but the shallow reading and writing of raw I/O. When you write a pointer to file, you are writing the address that is the content of the pointer, not the data that are the contents of the address pointed to. Because this address is unique to the program being run, it will not be meaningful when read from the file by another program. To write a linked data structure such as a linked list to file, you need to do it manually by either writing only the data and rebuilding the list when you read the file, or by working out some way of packing the data structure so that it can be easily unpacked back into the original structure. |
| All times are GMT -5. The time now is 1:54 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC