473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Question about Files.

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?
}

Jul 23 '05 #1
5 1089
"enki" <en*****@yahoo.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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.]

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Jul 23 '05 #2
On 6 Mar 2005 06:24:29 -0800 in comp.lang.c++, "enki"
<en*****@yahoo.com> wrote,
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.

Jul 23 '05 #3
> #include<iostream>
#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?

Jul 23 '05 #4
Thanks for the pointers, none of my books went into this topic. I will
have to take this area up as a study topic.

Jul 23 '05 #5

"enki" <en*****@yahoo.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
35
by: Stan Sainte-Rose | last post by:
Hi, What is the better way to save image into a database ? Just save the path into a field or save the image itself ? I have 20 000 images (~ 10/12 Ko per image ) to save. Stan
2
by: cjl | last post by:
PHPers: I am a total beginner, so please excuse my very simple question. I have been playing with PHP on Windows XP, and mostly using Notepad2 (http://www.flos-freeware.ch/notepad2.html) for...
13
by: Eric_Dexter | last post by:
All I am after realy is to change this reline = re.line.split('instr', '/d$') into something that grabs any line with instr in it take all the numbers and then grab any comment that may or may...
7
by: James | last post by:
Today my friend ask me a question about setting logsecond to -1. The following sentences were copied from DB2 InforCenter: "By setting logsecond to -1, you will have no limit on the size of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.