[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?
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.
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.
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.
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";
}