d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How To Copy The Contents Of A Tree To A File
Prev12
Add Reply New Topic New Poll
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Nov 9 2012 08:00am
Quote (Eep @ Nov 9 2012 04:45am)
I keep trying to think in terms of c++ (only way I can think atm) so the first thing that came to my mind for what I was thinking originally was the idea of like the libraries ifstream and ofstream.


Are those similar to serialization or different? Or do you know.


You shouldn't think in terms of a language because it's too specific. Serialization is merely a concept that has implementations in languages and thinking in terms of pointers and the like is drilling too far down.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 9 2012 01:18pm
I see. I looked up some more stuff on it but it still seems a bit foreign to me.

I looked up an example (usually learn better with them) of serialization in a C++ program (the demo for the BOOST library) and I couldn't quite tell what it was doing exactly. I think I need to know a bit more fundamental stuff before I step into this one.

But for my problem, in the original post, could I just get by using files? (ifstream, ofstream)?

Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 9 2012 01:25pm
it's really too bad you're doing all this in c++, since that makes it probably 10 times more complicated than it would be in Java or Python.

to answer your question -- yes, you can "get by" using files. in fact, saving data to a file is the end result of a lot of serialization code to begin with (the other place it often occurs is when bits need to be sent over the wire from one process to another, where the two processes may not even be on the same computer).

as for using ifstream/ofstream -- I'm sure that you'll end up using that in order to actually write the bytes to a file. but before you do that, you still need to address the issue of serialization format -- that is, how you plan to represent the state of your tree into something that can be written onto a file and read back into memory from a file.

but here's a fairly simple serialization protocol for you to consider:

Code
string serialize() {
 // takes this current treemap and writes it to a string formatted as such:
 //    "key1::value1, key2::value2, key3::value3 .... keyN::valueN"
}

void deserialize(string saveString) {
 // reads a saveString formatted as such:
 //    "key1::value1, key2::value2, key3::value3 .... keyN::valueN"
 // and populates this current treemap with the key-value pairs in the string
}


from there, you just need to figure out how to convert a string to bytes that can be written to a file, and the reverse of that, which ifstream/ofstream may be able to do for you.

This post was edited by irimi on Nov 9 2012 01:26pm
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Nov 9 2012 01:32pm
Quote (Eep @ Nov 9 2012 02:18pm)
I see. I looked up some more stuff on it but it still seems a bit foreign to me.

I looked up an example (usually learn better with them) of serialization in a C++ program (the demo for the BOOST library) and I couldn't quite tell what it was doing exactly. I think I need to know a bit more fundamental stuff before I step into this one.

But for my problem, in the original post, could I just get by using files? (ifstream, ofstream)?


Take the example that irimi gave you. You could use ifstream/ofstream to write a plain-text file that just contains the '::' / ',' delimited key-value pairs right to the file without any real formatting and then read it out again, inserting them into a new tree as you go. That's serialization at it's simplest.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 9 2012 01:36pm
Quote (irimi @ Nov 9 2012 02:25pm)
it's really too bad you're doing all this in c++, since that makes it probably 10 times more complicated than it would be in Java or Python.

to answer your question -- yes, you can "get by" using files.  in fact, saving data to a file is the end result of a lot of serialization code to begin with (the other place it often occurs is when bits need to be sent over the wire from one process to another, where the two processes may not even be on the same computer).

as for using ifstream/ofstream -- I'm sure that you'll end up using that in order to actually write the bytes to a file.  but before you do that, you still need to address the issue of serialization format -- that is, how you plan to represent the state of your tree into something that can be written onto a file and read back into memory from a file.

but here's a fairly simple serialization protocol for you to consider:

Code
string serialize() {
 // takes this current treemap and writes it to a string formatted as such:
 //    "key1::value1, key2::value2, key3::value3 .... keyN::valueN"
}

void deserialize(string saveString) {
 // reads a saveString formatted as such:
 //    "key1::value1, key2::value2, key3::value3 .... keyN::valueN"
 // and populates this current treemap with the key-value pairs in the string
}


from there, you just need to figure out how to convert a string to bytes that can be written to a file, and the reverse of that, which ifstream/ofstream may be able to do for you.


that makes sense. I just have to figure out how to do that in whatever language I am using, right? (Converting the string to bytes that can be written)
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 9 2012 02:09pm
as far as I can tell, ifstream and ofstream do exactly that.

http://www.cplusplus.com/doc/tutorial/files/
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 9 2012 02:56pm
Quote (irimi @ Nov 9 2012 03:09pm)
as far as I can tell, ifstream and ofstream do exactly that.

http://www.cplusplus.com/doc/tutorial/files/


hrm. I am trying to contrast the <fstream> stuff with stuff like:

http://www.boost.org/doc/libs/1_36_0/libs/serialization/doc/index.html

A lot of stuff in there is a bit over my head but I am trying my best to see what they are doing. I am guessing the objects of type Archive is where the actual saving is being done?

edit: I was looking at the TUTORIAL section. One of the first 2 examples.

This post was edited by Eep on Nov 9 2012 02:56pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 9 2012 03:25pm
If you're talking about what they're doing, then yes they're doing serialization. They may very well be using fstream at some point along the way to do it, but that doesn't really matter. What matters is the contract that they provide: that you can turn any arbitrary object into an Archive and that you can turn an Archive back into that object.

In many ways, you're overthinking it. It really is just that simple.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 9 2012 03:51pm
Quote (irimi @ Nov 9 2012 04:25pm)
If you're talking about what they're doing, then yes they're doing serialization.  They may very well be using fstream at some point along the way to do it, but that doesn't really matter.  What matters is the contract that they provide: that you can turn any arbitrary object into an Archive and that you can turn an Archive back into that object.

In many ways, you're overthinking it.  It really is just that simple.


Yeah probably. I always over think stuff.
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll