easy way to remove trailing '/n' from a c string

closed account (iw0XoG1T)
I am using ctime() and when it prints the string the last character is a '/n', which i do not want. Is there a standard function for removing this trailing new line?

here is my code if you care:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "error.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()try{
	if(true)throw Run::Error(2,"this is a test!");
	return 0;
}
catch(Run::Error& e){
	time_t rawtime;
	time(&rawtime);
  	ofstream ofs("errorfile.txt",ios_base::app);
	if(!ofs) return e.error_num();
	ofs<<ctime(&rawtime)<<'\t'<<e.error_num()<<'\t'<<e.what()<<endl;
	return e.error_num();
}
catch(...){
	return 1;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef ERROR_H
#define ERROR_H
#include <string>
#include <stdexcept>
#include <time.h>

namespace Run{

	struct Error{
		Error(int ii)
			:i(ii),message("No message was given."){}
		Error(int ii,std::string s)
			:i(ii),message(s){}

		std::string what(){return message;}
		int error_num(){return i;}

		private:
		int i;		
		std::string message;

	};
}
#endif 

Store the return value of ctime in a std::string and remove the last character
closed account (iw0XoG1T)
Is there anyway I could declare the time in my header so I could store the time in my class? I would like to do this without creating another source file?

edit: ignore I found an another solution.
Last edited on
Topic archived. No new replies allowed.