In writing a logger for an application, the datestamp ends up adding a new line.
The datestamp function is as follows:

string log_class::get_time() {
	time_t rawtime;
	time (&rawtime);
	string timestamp;
	timestamp = (ctime (&rawtime));
	return timestamp;
}

This outputs something like Tue Sep 6 14:58:35 2011 , but when called in the main function with logfile << get_time() << " " << logmessage_to_write << endl; , it produces a newline after the datestamp like:

Tue Sep  6 14:58:35 2011
 data goes here

I would like it to be on one line, as in Tue Sep 6 14:58:35 2011 data goes here , but I can't figure out how to do this. If anyone has an idea how to remove the newline, that would be great.

In between lines 5 and 6 try adding

timestamp.erase(timestamp.end() - 1);
commented: Thanks man! +2

That worked great. Can you explain why that works?

it erases the last element in the string which happens to be the newline. here is a good reference for working with strings.

Okay, that makes sense. One last question: Do you know how to format the time into something like yyyy-mm-dd hh:mm:ss? This is for a log function and it will make it easier to go through the log.

You might be able to give this a try.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.