Delphi Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreDelphi Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 26th, 2004, 08:04 AM
Canderel Canderel is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 40 Canderel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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:
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)

Reply With Quote
  #2  
Old July 26th, 2004, 10:58 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,383 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 20 h 49 m 30 sec
Reputation Power: 4080
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

Reply With Quote
  #3  
Old July 27th, 2004, 01:04 AM
Canderel Canderel is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 40 Canderel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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++.

Reply With Quote
  #4  
Old July 27th, 2004, 05:29 AM
Canderel Canderel is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 40 Canderel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreDelphi Programming > Reading last entry in text file

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap