Question about Files.

E

enki

I would like to know how to save and read an array of objects using
stdio.h.

So far I have opened and closed the file but how do I actually put the
data in the file then read it out?

This is what I have...

#include<iostream>
#include<stdio.h>

#include "myvec.cpp"
#include "an.hpp"

int main(){
int lp = 0;
int ag;
int num;

V<anmial*>box; //My personal vector.

while(ag != 0){

anmial *an;
cout<<"The age 0 quits:";
cin>>ag;
cout<<"1)Dog 2)Cat 3)Horse 4)Person:";
cin>>num;

if(num < 1){
an = new dog(ag);
}
if(num == 2){
an = new cat(ag);
}
if(num == 3){
an = new horse(ag);
}
if(num > 3){
an = new person(ag);
}

box=an;

lp++;
}

FILE *ptr;
ptr = fopen("Anmail_Data.dat", "wb");
for(int l = 0 ; l > lp; l++){
//What goes here?
}

fclose(ptr);
return 0;

Later, it how would I read the file?
}
 
L

Larry Brasfield

enki said:
I would like to know how to save and read an array of objects using
stdio.h.

That is a complex subject, generally known
as "serialization". Sometimes it is called
"object persistance" or even "pickling". I
suggest a web search rather than expecting
a tutorial here.
So far I have opened and closed the file but how do I actually put the
data in the file then read it out?

Typically, in ad-hoc solutions, objects are written
out, one sub-object at a time, preceeded by some
kind of type identifier if their type is not implicit,
and possibly accompanied by an identifier if other
serialized objects will be referring to it. Reading
is nearly the reverse, except that objects have to
be instantiated according to their type if not known
in advance.
This is what I have...

#include<iostream>
#include<stdio.h>

This is a strange combination. Do you mean to
use both C++ streams and C stream I/O?

[Cut code that does no (de)serialization.]
 
D

David Harmon

On 6 Mar 2005 06:24:29 -0800 in comp.lang.c++, "enki"
I would like to know how to save and read an array of objects using
stdio.h.

Although you have left out the juiciest parts of your code, I see that
you are using a (animal *) pointing at variously a dog, horse, etc..
Which implies that you have a inheritance hierarchy of objects and not
"Plain Old Data".

Dumping such objects directly to a binary file and trying to read them
back is futile, and ultimately undefined. Such objects must be
created by executing their respective constructors. To save and
restore them you must write some token that tells you what kind of
object to create, along with whatever constructor arguments it needs.

For systematic ways to do that, search "serialization". Although I am
not actually using it, I have confidence in the serialization library
from http://www.boost.org

But, if your classes remain simple enough, it may be easier to roll
your own by writing merely a text file with 1 for dog, and the age,
and reading back with code similar to what you already show.
 
E

enki

#include said:
#include<stdio.h>

I added to a pre-exiesting program. I was going through a tutorial at
work that I can only access there. They glossed over these points and
I didn't think it was so comples.

What would be a good book for this topic?
 
E

enki

Thanks for the pointers, none of my books went into this topic. I will
have to take this area up as a study topic.
 
H

Howard

enki said:
I would like to know how to save and read an array of objects using
stdio.h.

So far I have opened and closed the file but how do I actually put the
data in the file then read it out?

This is what I have...

#include<iostream>
#include<stdio.h>

#include "myvec.cpp"
#include "an.hpp"

int main(){
int lp = 0;
int ag;

You might consider using the whole word "age" as the variable name. Using
"ag" instead of "age" doesn't save much time or space, and makes it hard to
know what it means.
int num;

V<anmial*>box; //My personal vector.

Do you mean "animal"?
while(ag != 0){

You have not set ag to anything yet. Using before initializing it makes no
sense, and may cause your program to do just about anything, including
crashing.
anmial *an;
cout<<"The age 0 quits:";
cin>>ag;

Here, you get ag. But the code below will execute even if ag is zero,
because you don't check it's value here.
cout<<"1)Dog 2)Cat 3)Horse 4)Person:";
cin>>num;

if(num < 1){
an = new dog(ag);
}
if(num == 2){
an = new cat(ag);
}
if(num == 3){
an = new horse(ag);
}
if(num > 3){
an = new person(ag);
}

box=an;

What does this do? It looks like you're assigning the pointer [an] to the
entire vector [box]. Do you have an operator= for the V class that does
some kind of an insert in this case? If not, this shouldn't even compile.
lp++;
}

FILE *ptr;
ptr = fopen("Anmail_Data.dat", "wb");
for(int l = 0 ; l > lp; l++){
//What goes here?

Anything you want could go here. Probably a call to a member function of an
object. But what object? You didn't save them in your vector anywhere
above, at least as far as I can tell.

Not only that, but you are setting l to 0, then looping while it's bigger
than lp. But since lp is likely to be a fairly small positive integer, l is
never going to be bigger than lp, so the loop won't do anything at all.

By the way, "l" is a bad variable name. I can't tell if it's the small L
(as in language) or the capital i (as in India).
}

fclose(ptr);
return 0;

Later, it how would I read the file?
}

You decide yoursefl how to read or write data inside your class. Writing <<
and >> operators for the classes is one way. As others said, look for
information on persisting classes in C++. The site groups.google.com is
helpful, as well as most good books on C++.

By the way, how do you delete these objects when done with them? I see new
being used, but no matching calls to delete.

-Howard
 

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
474,202
Messages
2,571,055
Members
47,658
Latest member
jaguar32

Latest Threads

Top