How do u add text to an ostream&

Hi I've written a simple class Message which is meant to be an Email, Ive a print method which write to an ostream& file but I want to append the text to this file on each iteration, however it overwrites and only the last instance's info is writting out... here's d class Message.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * Message.cpp
 *
 *  Created on: 26-Nov-2008
 *      Author: Patrick Nevin: 06754155
 */

#include "Message.h"
#include <string>
#include <iostream>
using namespace std;
/*
 * @param from, string to contain who the message if from
 * @param to, string to contain who the email if for
 * Constructor, initialises the internal state of instances
 * of this object
 */
Message::Message(string from, string to):
	_sender(from),
	_recipient(to),
	_body(""),
	_message(""){
}
/**
 * @param s, string to append to the message body
 */
void Message:: append(string s) {
	_body.append(s);
}
/*
 * @return: a string corresponding to the entire message
 * of this email....
 */
string Message::to_string() {
    _message.append("From: "+_sender+"\n");
	_message.append("To: "+_recipient+"\n");
	_message.append("Body: "+_body+"\n");
	return _message;

}
/*
 * @param stream, an outputstream to contain the message
 * @return the stream containing the message
 */
void Message::print(ostream& stream) const {
    stream << _message;
}
void Message::print() const {
	cout << _message;
}
Message::~Message() {
	// TODO Auto-generated destructor stub
}


and the main....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * main.cpp
 *
 *  Created on: 10-Nov-2008
 *      Author: Patrick Nevin: 06754155
 */

#include <iostream>
#include <fstream>
#include <vector>
#include "Message.h"
using namespace std;

int main() {

	//read in the student data file
	fstream messageStream("email.dat");
	//create a vector of Message objects
	vector<Message> messages;

	//while there are more words in the stream, loop
	while (!messageStream.eof()) {
		//string to hold the info we read in;
		string line, from, to, message;
		//read in the from part
		getline(messageStream,line);
		from = line;
		//read in the to part
		getline(messageStream,line);
		to = line;
		//read in the message
		getline(messageStream,line);
		message = line;
		//create a new instance of Message
		Message *m = new Message(from, to);
		//invoke the append method on m
		m->append(message);
		//invoke the to string method on m
		m->to_string();
		//And add it to the vector
		messages.push_back(*m);
		//delete the pointer
		delete m;
	}//end while()
	messageStream.close();

	//Create an iterator to go over each element/Employee
	//in the "workers" vector
	vector<Message>::iterator it;
	for (it = messages.begin(); it != messages.end(); it++) {
		//set curent to the current Employee
		Message current = *it;
		//and invoke its print method
		//current.print();
		ofstream file("C:/Message.txt");
		current.print(file);
		file.close();

	}
	return 0;
}

Hope someone can show me how to append the text probably in my print() methods so the full file email.dat is writting out and not just the last 3 lines thanks
1
2
3
4
5
6
7
8
9
patrick.nevin@ucdconnect.ie
linda.shannon@ucdconnect.ie
This is my message.
patnevin1960@hotmail.com
outlaw323@hotmail.com
And this is my message in return.
robert.plunkett@ucdconnect.ie
patricknevin212@gmail.com
This is the last message .
By default, files are truncated when opened. To prevent this, pass std::ios::app to the flags: std::ofstream file("email.dat",std::ios::app);
Thanks you helios that was exaclty what i was looking for, much appricaited :-)
Topic archived. No new replies allowed.