The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Delphi Programming
|
Reading last entry in text file
Discuss Reading last entry in text file in the Delphi Programming forum on Dev Shed. Reading last entry in text file Delphi Programming forum discussing Delphi related topics including Kylix, C++ Builder, and more. Delphi is a high-performance language, originally based on the PASCAL language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 26th, 2004, 08:04 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 40
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
Reading last entry in text file
Hi, I'm using C++ Builder.
I am writing a program which creates text files. But to ensure a continuity between the text files I need to check the last one I've written (typically not in the same session that I'm in now) to see if the one I'm writing now overlaps the old one. If it doesn't I know data is lost somewhere.
Anyway, what I want to know is, how can I open the very last line of the text file without having to read the whole file, because sometimes the files can become quite large.
I am not familiar with C++'s filehandling. I am not sure I'd be able to do it in delphi either, but anyway. Help will be appreciated.
Very simplified example. Lets say my text file writes a counter, file 1 is:
Now if I want to write file 2, I want to check that my first entry in file 2, is in file one. So lets say file 2 is:
then I search through file 1 till I find a 5, then I know I have continuity. (or I could take the last entry from file 1, and see if it occurs within file 2. -> This is my plan)
|

July 26th, 2004, 10:58 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
There are a few ways to do this, but the nicest way would be something like this:
1. Use fopen() to open the file
2. Use fseek() to position your read pointer to say 10 bytes from the end of the file.
3. You can now easily read the last 10 bytes (fread)
4. Now parse backwards for \n (strrchr).
5. Once you find the position for \n from the back, you can figure out the last entry.
UNTESTED CODE
Code:
#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);
}
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|

July 27th, 2004, 01:04 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 40
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
Haven't tested it out either (yet), but a question so long:
SEEK_END What's that? (Eof?)
What does that very last line do?
copy whatever is left into buf2? But how? Won't it have too much info in it?
ie.
buf2 = "78910\eof<(#$@!$%$#'*@"
Ok, but then I parse it to the \eof char. I guess.
I am not 100% sure how this'll work, but the file accessing is more what I needed, I can probably get along from there.
Thanks very much! I really appreciate it, as I can not in the least, do file accessing in C++.
|

July 27th, 2004, 05:29 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 40
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
Tested it by now...
Had some problems, as the fread command needs the value to be negative when reading from the end of the file.
But other than that, it worked great. Thank you so much.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|