Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 21st, 2006, 9:25 AM   #1
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 242
Rep Power: 3 Seif is on a distinguished road
Classes in fstream

Hi just a quick question. I have a class called person, of which I would like to save to a file. I was wondering if I could save it to a file as I would a struct:

fin.read((char *)(&newPerson), sizeof(Person));

and to load it again:

fout.write((char *)(&newPerson), sizeof(Person));

Have tried a couple of times but can't seem to get it to work. Is it actually possible? I really dont want to save attributes of each object on a seperate line if possible.

Cheers in advance.

Seif.
Seif is offline   Reply With Quote
Old May 21st, 2006, 9:46 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
In C++ a class and a struct are essentially the same thing. The difference is default exposure (public, private, etc.). The problem with saving them to disk is that they might not look like what you think they look like. Compilers tend to align members on "even" boundaries, with the result that there are unused areas of padding. "Even" boundaries depend on the underlying architecture of the machine. Most compilers allow you to tell them not to do this. The saving is bytes, the cost is performance. Even if you get it right it might not work portably. It's what serialization is all about.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 21st, 2006, 2:20 PM   #3
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 242
Rep Power: 3 Seif is on a distinguished road
Thanks DaWei, I've done a read of object serialization and found exactly what I needed. I knew the similarities of structs and classes but weren't sure if i was possible, and believe it or not my lecturer didn't know either when i asked him thank god for the PFO!
Seif is offline   Reply With Quote
Old May 21st, 2006, 3:51 PM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by Seif
and believe it or not my lecturer didn't know either when i asked him
That should be pretty basic stuff for a lecturer. (I could be wrong, as I'm not.) I always asked questions they didn't know, but as you can imagine, they were on slightly more advanced topics.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old May 21st, 2006, 3:59 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Don't get me started .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC