Archived

This topic is now archived and is closed to further replies.

ofstream problem, logfile not working correctly

This topic is 5144 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have the following logfile class, defined like so:
#include <ctime>
#include <cstdlib>
#include "LogFile.h"

namespace Engine {
	LogFile :: LogFile (const std :: string& fname) {
		filename = fname;
		opened = false;
	}

	LogFile :: LogFile (const LogFile& log) {
		filename = log.filename;
		opened = log.opened;
	}

	LogFile :: LogFile () {
		filename = "";
		opened = false;
	}

	LogFile& LogFile :: operator = (const LogFile& log) {
		filename = log.filename;
		return *this;
	}

	LogFile :: ~LogFile () {
		// Can''t throw an exception from a destructor, so just don''t bother writing
		// the date and time if anything goes horrifically wrong.
		time_t currenttime = time (0);
		if (filename != "") {
			stream.open (filename.c_str ());
			if (stream.bad () == 0) {
				stream << "Logfile closed at -> " << ctime (¤ttime) << "\n";
			}
			stream.close ();
		}
	}

	bool LogFile :: operator == (const LogFile& log) const {
		return (filename.compare (log.filename) == 0);
	}

	bool LogFile :: operator != (const LogFile& log) const {
		return (filename.compare (log.filename) != 0);
	}

	inline void LogFile :: setFile (const std :: string& file) {
		filename = file;
	}

	void LogFile :: write (const std :: string& line) throw (Exception) {
		if (filename == "") {
			throw Exception ("[LogFile]: empty filename specified.");
		} else {
			stream.open (filename.c_str ());
			if (stream.bad () != 0) {
				throw Exception ("[LogFile]: unable to open file.");
			} else {
				if (opened == false) {
					opened = true;
					// First time opened, so give compulsory date and time line.
					time_t currenttime = time (0);
					stream << "Logfile opened at -> " << ctime (¤ttime) << "\n";
				}
				stream << line << "\n";
			}
			stream.close ();
		}
	}

	inline std :: string LogFile :: getFile () const {
		return filename;
	}

	std :: string LogFile :: toString () const {
		return filename;
	}

	double LogFile :: hashCode () const {
		// TODO
		// Should probably use std :: ststream.
		int ret = atoi (filename.c_str ());
		return (ret >> 2) * 0.75;
	}
}
I use it like this:
#include <iostream>
#include "LogFile.h"

int main (int argc, char **argv) {
	Engine :: LogFile* lfile = new Engine :: LogFile ("tlfile.txt");
	try {
		lfile->write ("Test line one...");
		std :: cout << "one\n";
		lfile->write ("Test line two...");
		std :: cout << "two\n";
		delete lfile;
	} catch (const Engine :: Exception& exception) {
		std :: cout << exception.toString () << "\n";
		delete lfile;
	}
	return 0;
}
Neither of the two test lines I attempt to write are actually written to the file, which after the program exits only contains the date and time the class was destructed at (see destructor). I''ve looked at this for a while, and everything seems to look OK, but I hardly ever use C++ style streams, so this isn''t necessarily an indication of the correctness of the program :-) Can anyone see what I''ve done wrong? TIA

Share this post


Link to post
Share on other sites
You need to open the file in append mode. Otherwise, each time the file is opened, it is truncated. Thus, all you see is whatever was written last.

Add ios::app as the mode parameter.

Share this post


Link to post
Share on other sites