possible to save things like classes,integers etc into files instead of strings?

Started by
16 comments, last by johnnyBravo 20 years, 1 month ago
quote:Original post by johnnyBravo
so i guess i should just store things as strings then?

the thing is tho, i wouldnt be saving pointers, just classes and other data types.

i''d so rather do that, but i guess everyone is saying that is a bad idea.

But this program would probably only be used with ms vc++6 anyway...


But if you are saving classes with pointers to other classes, that''s a bad idea. Same thing as storing a linked list or an hash table just as they are on memory.
When i need to store stuff on a file which references other data i use ID''s to identify the diferent objects. The ID is the cell number of that object on the file. You can then later load the objects from the file and re-link them in memory.
I once made a nice generic memory manager for something like this. It was transparent to the user how the objects were saved and loaded. Probably it wasn''t very efficient but it worked.
- blew
Advertisement
quote:Original post by Zahlman
You might also want to have a look at how the Serializable interface works in Java.

Serializable doesn''t "work" - it''s just a tagging interface. Serialization in Java uses reflection, which is a facility that''s not available in C++.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
quote:Original post by Blew
He DID try to help.

Surely you jest. If you have no clue, you''re more helpful if you keep your mouth shut. Clueless advice is worse than no advice at all.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
quote:Original post by johnnyBravo
hi, im wondering if there are any functions that i could use in c++ on saving and reading files that can hold things like classes, integers, floats etc instead of just holding strings.

So i can read the values straight into the right storage types etc.


Thanks,


fwrite(..) (stdio.h) can write anything you have a pointer for in a file in binary format. Then you can read it with fread().

Arild Fines: What's your contribution to this thread? Your posts bother me most than the Xeneth's one.

[edited by - owl on February 29, 2004 10:06:10 AM]
[size="2"]I like the Walrus best.
This process is called serialization. It is used whenever an object has to be converted to a stream, or from a stream to an object (deserialization). Im sure you''ll find plenty of helpful tutorials on serialization on google.
"I never let schooling interfere with my education" - Mark Twain
I find that until you have your code thoroughly debugged and working, the optimization that this gives you isn''t worth the cost of not being able to visually read the contents of the file in your favorite editor.

It is possible and fairly easy to dump your non-poionter, non-reference data into binary though.
quote:Original post by owl
Arild Fines: What''s your contribution to this thread?

None, but at least I''m not offering bad advice. The original question was answered by the third post - the only interesting about this thread now is the meta-discussion.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
quote:Original post by ParadigmShift
quote:Original post by Xeneth
I think it's posible but I'm no expert. Not sure how (haven't delt with reading and printing to file). Why, for saving games?

Technology can either save us or destroy us. Me ... I say it will destroy us if Microsoft is the example.


Is this the most useless post ever or is it just me?


NO it's not just you. I couldn't sleep and that was the result "look at the time".

As for the clueless advise being worse then none. That wasn't advice, that was confromation meining that there is a way and he's on the right track. notice I didn't try telling him how to or that he should or shouldn't. I also asked why so incase I come across the same ressoning or problem.




Technology can either save us or destroy us. Me ... I say it will destroy us if Microsoft is the example.



[edited by - Xeneth on March 1, 2004 7:22:08 AM]
Will you play with me? (Grins Evilly)

This topic is closed to new replies.

Advertisement