Last line or entry in a file
Code:
Does anyone know how I can read the last entry in a file? I have a file that I receive everyday but I want to only be able to report out the last entry or display the last entry as the file is quite large but new entries get written to it on a daily basis.
In such I will like it to display only entries for each current day in the file.
I have downloaded the Bloodshed Dev-C++ and have tried using this program which I found online
#include <stdio.h>
#include <string.h>
#define MYSIZE 10
FILE *fp;
char buf[MYSIZE + 1], buf2[MYSIZE + 1];
char *ptr;
fp = fopen("filename", "r");
fseek(fp, MYSIZE, SEEK_END);
fread(buf, sizeof(char), MYSIZE, fp);
fclose(fp);
ptr = strrchr(buf, '\n');
if (ptr) {
// We've found the data
strcpy(buf2, ptr+1);
}
Buy honestly I dont have a clue. I have compiled it and run it and it stops at
fp = fopen("filename", "r");
I have put in the following
fp = fopen("ggg.txt", "r");
but it still does not work?
Help
Thanks