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
/*
* Message.cpp
*
* Created on: 26-Nov-2008
* Author: Patrick Nevin: 06754155
*/
#include "Message.h"
#include <string>
#include <iostream>
usingnamespace 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
}
/*
* main.cpp
*
* Created on: 10-Nov-2008
* Author: Patrick Nevin: 06754155
*/
#include <iostream>
#include <fstream>
#include <vector>
#include "Message.h"
usingnamespace 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 .