Here's a bit of code of what i had in head
Code:
#include <stdio.h>
#include <string.h>
#define LARGE_ENOUGH_VALUE 12l // But not to large either...
int main(void)
{
FILE *file;
char buffer[LARGE_ENOUGH_VALUE];
char *ptr;
if ((file = fopen("test.txt", "rt")) != NULL)
{
if (fseek(file, -LARGE_ENOUGH_VALUE, SEEK_END) != 0)
printf("Error: fseek returned nonzero value\n");
else
{
if (fgets(buffer, LARGE_ENOUGH_VALUE, file) == NULL)
printf("Error: fgets returned NULL\n");
else
{
if ((ptr = strrchr(buffer, ' ')) == NULL)
printf("Error: no space in the line read\n");
else
{
// Here you could think about removing the '\n' if there is one
printf("The last word is: %s\n", ptr + 1);
}
}
}
}
return 0;
}
Anyway. There's more systematic solution like the one Salem proposed (ie, checking if the line you just read is really the last line of the file, which is something my code isn't doing) but if your log file have a really "standard structure", the solution i gave could also work. But it still would be good to check if the line you read with fgets is really the last line of the file... and since this is not difficult to implement, well, you know what you have to do now.