Data Saving/Loading & Object Construction

J

Jerivix Entadi

I'm attempting to create an application to work with a fluid database
of people. I'm doing this in a command line, text-based manner. I need
to know two things: 1) How do I save data, perhaps a group of objects
and their memebers or a few lines of text? and 2) Is there any way to
automate object construction? I'm thinking that I can create a vector
or something similar to handle the number of people, is there a way
for me to write a function that will create a default object for each
of these people?

Any answers or directions to answers would be greatly appreciated.
 
J

John Harrison

Jerivix Entadi said:
I'm attempting to create an application to work with a fluid database
of people. I'm doing this in a command line, text-based manner. I need
to know two things: 1) How do I save data, perhaps a group of objects
and their memebers or a few lines of text?

It's up to you how you save the data. I think you're asking about whether
you should save as binary data or text. This really depends on what you want
to do with the data. Binary would be the traditional approach but these days
XML should be considered since it will enable your data to be processed by
all the XML parsers that are out there.
and 2) Is there any way to
automate object construction? I'm thinking that I can create a vector
or something similar to handle the number of people, is there a way
for me to write a function that will create a default object for each
of these people?

I don't really understand this question either. You can write any
constructor you like, does that count as automatic object construction?
Any answers or directions to answers would be greatly appreciated.

I think a bit more detail in the question will get you more help.

john
 
J

Jerivix Entadi

John Harrison said:
It's up to you how you save the data. I think you're asking about whether
you should save as binary data or text. This really depends on what you want
to do with the data. Binary would be the traditional approach but these days
XML should be considered since it will enable your data to be processed by
all the XML parsers that are out there.


I don't really understand this question either. You can write any
constructor you like, does that count as automatic object construction?


I think a bit more detail in the question will get you more help.

john


Let me carify a bit. I don't know how to even save as binary data or
XML. I've done a bit of experimentation with saving single lines of
text, but past that I really don't know much.

As far as object creation goes, I'm not worried about the constructor
itself, but rather defining the objects in the first place. Is there
any way to write soemthing that will create the objects for me?

In other words, if I have a Class People, and the user inputs "Betty",
can I then pass "Betty" into a function that will create an object for
Betty and let the constructor etc. take it from there?
 
T

Thomas Matthews

Jerivix said:
Let me carify a bit. I don't know how to even save as binary data or
XML. I've done a bit of experimentation with saving single lines of
text, but past that I really don't know much.

As far as object creation goes, I'm not worried about the constructor
itself, but rather defining the objects in the first place. Is there
any way to write soemthing that will create the objects for me?

In other words, if I have a Class People, and the user inputs "Betty",
can I then pass "Betty" into a function that will create an object for
Betty and let the constructor etc. take it from there?

Search the newsgroups and web for "Design Pattern Factory".
This is a pattern used for creating instances given an indentifier
of some sort.

As for writing objects, the trick is to have each object provide
a method (or overload an existing operator) to write (and read)
each member. If the member is not a simple type, then it should
have read and write methods too.

The methods to use are the I/O streams. Also search the newsgroups
and web for "Serialization OR permanent C++". See the C++ FAQ:
http://www.parashift.com/c++-faq-lite/serialization.html

One should always research the FAQ and search the newsgroups
(as well as the web) before posting. You can search for
"Thomas Matthews binary write" for some replies I've posted.
{Although I have improved the code a bit since then.}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
J

John Harrison

Jerivix Entadi said:
"John Harrison" <[email protected]> wrote in message


Let me carify a bit. I don't know how to even save as binary data or
XML. I've done a bit of experimentation with saving single lines of
text, but past that I really don't know much.

File I/O (binary or text) is done in C++ with the iostream library. That's
rather a big topic to cover in a newsgroup post.
As far as object creation goes, I'm not worried about the constructor
itself, but rather defining the objects in the first place. Is there
any way to write soemthing that will create the objects for me?

I'm sure there is.
In other words, if I have a Class People, and the user inputs "Betty",
can I then pass "Betty" into a function that will create an object for
Betty and let the constructor etc. take it from there?

Seems a bit confused, you say 'create an object for Betty and let the
constructor etc. take it from there' but it is the constructors job to
create objects, nothing else's.

You can certainly write a function that given a persons name will create an
object for that person.

Person create_person(std::string name)
{
// create the person here
}

Of course you have to fill in the part indicated by the comment 'create the
person here'

But I sense you are asking for something more, since the above code is
completely straightforward. Unfortunately I'm still not sure what that is.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top