Checking the contents of a template object.

[EDIT] I've updated the title of this thread to indicate the direction that it started going.

I'm making a logging function for which I'd like to add a timestamp to the start of each lime.

To do this I think I need to read the last character of the file I am appending and check it if is a \r or \n. How does one determine the last character, or the address of the last character in a file using fstream?

Example code:
1
2
3
4
5
6
7
8
9
10
11
12
std::fstream logFile("MyLog.txt",std::fstream::in | std::fstream::app);

template<class T>
logger& operator<<( logger& log, const T& output ) {
  if (logFile.lastChar() == '\n' || logFile.lastChar() == '\r')
    logFile << "[Line Preamble] ";

  logFile << output;
  logFile.flush();

  return log;
}


In this case logger& is a class with a friend function that R0mai helped me with. The logFile.lastChar() function is currently fictional and is what I'm trying to evaluate.

The other option is that I could just keep track of output and determine if it contains a '\n' '\r' or std::endl, but this would not let me add the preamble to the first line when I start the program. Also, I've tried to evalute (output == std::endl) and this gives a compiler error.
Last edited on
Actually, evaluating the value of const T& output seems like the way to go if I am looking for something efficient. However I am not very experienced with template<class T> stuff.

I can compile if((char)output == '\n') however don't think that this is giving me what I want. I think I need to know if output contains '\n', but I'm not sure how to cast and then search it.
I've tried evaluating the contents of const t& output by checking the binary itself.
1
2
3
4
	std::cout << output;

	for (int i= 0; i < sizeof(output) ; i++)
		std::cout << std::hex << ((*output >> i*8) & 0xff) << "|";

If I input "abcd\n", my output is:
abcd
61|0|0|0|61|0| 


This means that if evaluate the individual bytes of output it does not appear to be in an ASCII format. ARGH!
Last edited on
The other option is that I could just keep track of output and determine if it contains a '\n' '\r' or std::endl, but this would not let me add the preamble to the first line when I start the program
let the logger assume that the last character was a line break.

I think I need to know if output contains '\n', but I'm not sure how to cast and then search it.
¿When could 'output' contain a line break? (¿what type?) you will need to threat those cases in a special form.

You could also use a 'put_time_stamp' method
It took a few extra hours, but I got it:

To check if the newline character is in a template do the following:
1
2
3
4
5
template<class T>
logger& operator<<( logger& log, const T& output ) {
  for (int i = 0; i < sizeof(output); i++)
    if (output[i] == '\n') cout << "a new line has been found";
}
Last edited on
Wait, that only works if we have a set of character inputs. The moment I put in ints or doubles, things die again. Gah!
sizeof() is a string function. You'll have to typecast anything that you put into it as a string, like string(x);
Topic archived. No new replies allowed.