vector question

K

Kitty

Hi, I have a question about vector.

Suppose that I have "vector<Object>" container, where Object is some class.
I would like to output this vector<Object> into a file to save. Then, in the
next time, I can re-load this data if necessary.

Could someone provide me some info?

Thanks.
 
J

Jon Bell

Suppose that I have "vector<Object>" container, where Object is some class.
I would like to output this vector<Object> into a file to save. Then, in the
next time, I can re-load this data if necessary.

C++ doesn't have any standard tools for doing this, so you have to do
everything yourself.

First decide the file format. That is, how is an object of class Object
going to be represented in the file, and how will you arrange the sequence
of objects in the file? For example, you might use a plain text file,
with one line per object, and with the member data written in text format,
separated by tabs.

Second, write a function (probably a member function of class Object,
perhaps a friend function) that writes one object into one line of the
file.

Third, write a function that loops over the objects in the vector, and
invokes your object-writing function for each object.
 
A

assaarpa

Do you know any library/tool providing a function for my purpose?

Yea, you can use std::fstream for example to write into a file look up from
the INTERNET!!!1 how that works.
 
K

Kitty

Do you mean that fstream can be used to write the "whole" vector<Object>
into a file?

Thanks.
 
K

Kitty

The reason why I need to copy my container to a file is for the backup
purpose. If the program crashes, I can reload the data and prevent to repeat
the whole simulation.
 
C

Chris Dams

Dear Kitty,

Kitty said:
Do you mean that fstream can be used to write the "whole" vector<Object>
into a file?

No, as mentioned earlier, you will have to invent your own file format
for this. One possibility is to use XML. There are libraries to read and
write XML-files.

Best,
Chris
 
K

Kitty

Thanks all.

I am using Visual C++ 6.0. Any built-in function to my purpose? Actually my
program is just a console (DOS) application.
 
A

Arijit

Kitty said:
The reason why I need to copy my container to a file is for the backup
purpose. If the program crashes, I can reload the data and prevent to repeat
the whole simulation.

Actually, if thats all that you want, you can redirect standard input to
take input from a file instead of the console. Exactly how to do it may
vary with your OS, but is similar on both Windows and Linux.

-Arijit
 
D

Dave Townsend

Kitty said:
Thanks all.

I am using Visual C++ 6.0. Any built-in function to my purpose? Actually my
program is just a console (DOS) application.
You might want to look at MFC serialization functionality if you are using
VC++6. Basically, you
can derive you object from the MFC base class and write your own
serialization methods.
There's quite a bit of support in MFC for serialization. Look in the VC
help.
 
K

Kitty

Is it correct that I directly write an object by using the following code:

Object obj;
.........
ostream outfile("file.dat");
outfile.write((char *)&obj, sizeof(obj));
 
J

Jon Bell

Is it correct that I directly write an object by using the following code:

Object obj;
........
ostream outfile("file.dat");
outfile.write((char *)&obj, sizeof(obj));

If your object contains pointers that point to other data, or if it
contains objects that contain pointers that point to other data, this will
not copy the "other data" and therefore fail.
 
R

rossum

Hi, I have a question about vector.

Suppose that I have "vector<Object>" container, where Object is some class.
I would like to output this vector<Object> into a file to save. Then, in the
next time, I can re-load this data if necessary.

Could someone provide me some info?

Thanks.

There is no built in way to do what you want, you will need to write
things yourself. There is some help in the FAQ,
http://www.parashift.com/c++-faq-lite/, search for "serialization".

To get you started here is part of one possible solution:

rossum

----------------------------------

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>

using std::string;
using std::vector;
using std::eek:stream;
using std::eek:fstream;

//-------------------------------------

class My_Object {
private:
int m_int;
string m_string;
double m_double;

public:
My_Object (int i, const string& s, double d) :
m_int(i), m_string(s), m_double(d) {}

friend ostream& operator<<(ostream& os, const My_Object& obj);
}; // end class my_Object

//-------------------------------------

ostream& operator<<(ostream& os, const My_Object& obj) {
const char delimiter = '|';
os << obj.m_int << delimiter
<< obj.m_string << delimiter
<< obj.m_double << delimiter;
return os;
} // end operator<<()

//-------------------------------------

void save_restart_data(ofstream& ofs, const vector<My_Object>&
obj_vec) {
for (int i = 0; i < obj_vec.size(); ++i) {
ofs << obj_vec << '\n';
} // end for
return;
} // end save_restart_data()

//-------------------------------------

int main() {
const char* restart_file_name = "restart-data.txt";
vector<My_Object> obj_vec;

obj_vec.push_back(My_Object(1, "first", 1.1));
obj_vec.push_back(My_Object(2, "second", 2.2));
obj_vec.push_back(My_Object(3, "third", 3.3));

ofstream restart_file;
restart_file.open(restart_file_name);
if (!restart_file) {
std::cerr << "Unable to open "
<< restart_file_name << ".\n";
}
else {
save_restart_data(restart_file, obj_vec);
std::cout << "Restart data saved in "
<< restart_file_name << ".\n";
} // end if

system("pause");
return EXIT_SUCCESS;
} // end main()

//-------------------------------------
 

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

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top