I have a problem where I need to read lines from a log file, check if the line contains the word "fail" and if so output how many seconds passed from midnight to the time the error occured.
An example line from a logfile looks like this:
Aug 20 10:20:57 kernel: ata3: SATA link down
I have the first two parts of the problem workig correctly (reading lines from the logfile and checking if they contain the word "fail") but I'm a bit stumped on how to extract the time from the logfile and converting it into seconds passed since midnight.
What I have so far looks like this (the relevant part of the code):
do
{
currLine = fgets(buff, sizeof(buff), inFile);
if(strstr(currLine, "fail") != NULL)
{
/*convert the time into seconds since midnight and output it*/
}
} while(!feof(inFile));
So far I have only been able to come up with one idea of how to implement it, but it doesn't work (this would be inside the if statement above, where I have placed the placeholder comment explaining what it needs to do):
int hr, min, sec, timePassed;
fscanf(currLine, "%d:%d:%d", &hr, &min, &sec);
timePassed = hr * 3600 + min * 60 + sec;
Thanks very much in advance