saving a vector to a file

I have just tried to save data to a file from a program for the first time and I need to store a vector this is my code
1
2
3
4
vector<staffMember*> _allEmployees = com->getCollection();
			myfile.open("save.txt");
			myfile << _allEmployees;
			myfile.close();

this is the error message Im getting
Error	1	error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion)

I know basically nothing about writing to files what should I do
Without a bit more of code, it's unlikely anyone can help you, buddy....

what is staffMember*, for instance?
Last edited on
staffMember is a base class it has two derived classes inheriting from it all three of them are linked via agregation to the class company _allEmployees is a vector of pointers to staffmembers <staffMember*> which represents a list of all the staff members currently held by company what I am trying to do is save this list so that it is possible to load the information the next time the program is run
first of all: please, give me some air! one or two periods from time to time won't hurt! ;)

Now, your explanation is a bit confusing, cause staffMember can't be a class and a list of all the staff members at the same time... I guess IN staffMember, there is a kind of container with all the staff members. Could you post the definition of staffMember class?

Last edited on
staffMember is a class and nothing more _allEmployees is the list. the list is made up of staffMember objects or more precicely pointers to staff member objects. there is nothing wrong with the staffMember class or the list all I need to know is the correct syntax for saving a vector to a file
because
1
2
3
myfile.open("save.txt");
myfile << _allEmployees;
myfile.close();

dosent work
staffMember is an empty class? why on earth would you want an empty class? you need to store the names of the staff, then you do a vector <string> _allEmployees;

Well, perhaps I'm missing something. Anyway, to understand the syntax working with files: myfile is a fstream -> file stream. It works exactly as if you were working with iostream, i. e. as you were using "cout <<", but instead of you monitor, the receiver is the text file. of course, you cannot output "a class", or a pointer to a class, but rather "a variable" stored in the class.

You don't "save a vector" to a file, you write the content of the vector in a file. Vector is a container, therefore you can't "<< _allEmployees", but rather
1
2
3
for (int n = 0; n <_allEmployees.size(); n++){
myfile << _allEmployees [n].myVariable <<endl;
}


You should take a walk to the precious tutorial of this site:
http://www.cplusplus.com/doc/tutorial/files/

it can't be better explained..
Last edited on
so I have to do this?
1
2
3
4
5
6
7
8
9
10
11
12
		vector<staffMember*> _allEmployees = com->getCollection();
		myfile.open("save.txt");
		myfile << endl << com->get_cName() << "Payroll report" << endl;
		myfile << endl << "Name\tID\tType\tWage" << endl << endl;

		for(unsigned int i = 0; i < _allEmployees.size(); i++)
		{
			staffMember* sta = _allEmployees.at(i);

			myfile << sta->get_name() << "\t" << sta->get_id() << "\t" << sta->get_type() << "\t" << sta->wage() << endl;
		}
		myfile.close();
how do I know wheather it worked or not there were no errors the code compiled but I dont know how to check that it actually saved
If it compiled, you just need to run the program, look for the text file, open it, and check if everything was written as expected...

Does it compile? this line seams weird to me...:

1
2
staffMember* sta = _allEmployees.at(i);
}


being _allEmployees a vector of pointers of staffMembers....

it compiles fine that line simply says that the pointer sta has the values from position i in the vector _allEmployees I found the text file it wrote perfectly.
one more question would this method that I have used work for writeing to other types of file, for example a comma separated values file (.csv)
depends on the type of your file. With .csv it should be fine, it's still a simple text file.
Last edited on
Topic archived. No new replies allowed.