|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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: Code:
1 2 3 4 5 6 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: Code:
5 6 7 8 9 10 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) |
|
#2
|
||||
|
||||
|
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 Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month |
|
#3
|
|||
|
|||
|
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++. |
|
#4
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Delphi Programming > Reading last entry in text file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|