DanEgli
asked on
best way to isolate record # in a flat file.
Ok folks. I'm writing a program that reads data from a datafile that is generated within the system. Since it's a standard delphi typed file handle, I am using filepos, filesize, and seek to move to record numbers. However, for some reason, my code keeps seeking me to record #0 or #1 or #2. I can never get to #2 or above. Here's an illustration of what I'm doing:
type datafilerecord = record
deleted : boolean;
first_name: string[30];
last_name: string[30];
end;
In my function to move to the next record, I have this:
procedure TDvdCatForm.NextRecBtnClic k(Sender: TObject);
var thefilepos : integer; // for debugging
begin
VerifySaveRec;
TheFilePos := filepos(moviefile); // for debugging
LoadRecord(filepos(moviefi le)+1);
end;
The first time I press button named NextRecBtn, I get record #1 (ok), Next time I press the button, I could get #2 (ok) or I could get #1 again (WHAT!?)
Please don't tell me to use BDE or ODBC or anything. I have no experience in them and frankly I don't want to use them.
What am I doing wrong?
Thanks!
type datafilerecord = record
deleted : boolean;
first_name: string[30];
last_name: string[30];
end;
In my function to move to the next record, I have this:
procedure TDvdCatForm.NextRecBtnClic
var thefilepos : integer; // for debugging
begin
VerifySaveRec;
TheFilePos := filepos(moviefile); // for debugging
LoadRecord(filepos(moviefi
end;
The first time I press button named NextRecBtn, I get record #1 (ok), Next time I press the button, I could get #2 (ok) or I could get #1 again (WHAT!?)
Please don't tell me to use BDE or ODBC or anything. I have no experience in them and frankly I don't want to use them.
What am I doing wrong?
Thanks!
maybe you should use a var for storing your current pos,
instead of seeking relative
instead of seeking relative
Hi,
Please show the code for VerifySaveRec and LoadRecord
rondi.
Please show the code for VerifySaveRec and LoadRecord
rondi.
Is this flat file a line sequential file.
If so, then you could make a Schema-File (=recordlayout)
and use the file like a DB-Table (with the TTable-Comp).
You can then write Queries, Locate records, Move to records, Append Records, ... (Deletion of records is then not possible).
If you need a demo project, let me know.
Best regards,
The Mayor.
If so, then you could make a Schema-File (=recordlayout)
and use the file like a DB-Table (with the TTable-Comp).
You can then write Queries, Locate records, Move to records, Append Records, ... (Deletion of records is then not possible).
If you need a demo project, let me know.
Best regards,
The Mayor.
ASKER
Ok. To answer the questions. It is not a line sequential. It is a file of records.
As far as storing current position in a var, thats fine, but I still need an efficent way to seek a particular record and then overwrite it.
Code for VerifySaveRec and LoadRecord:
procedure TDvdCatForm.VerifySaveRec;
begin
if (itemunsaved) then
if (Application.MessageBox('Y ou have not saved this entry. Do you wish to save it now?', 'Exit Confirmation', MB_YESNO+MB_ICONQUESTION) = 6) then
SaveScreenRecord;
end;
procedure TDvdCatForm.savescreenreco rd;
begin
movie.deleted := dvdcatform.CheckBox1.Check ed;
movie.Movie_ID := dvdcatform.MovieID.text;
movie.Title := dvdcatform.MovieName.Text;
movie.Format := dvdcatform.MovieFormat.Ite mIndex;
movie.Genere := dvdcatform.MovieGenere.Ite mIndex;
movie.Location := dvdcatform.MovieLocation.t ext;
if not filepos(moviefile) = filesize(moviefile) then
seek(moviefile, filepos(moviefile));
WRITE(moviefile, movie);
itemunsaved := false;
end;
procedure tDvdCatForm.loadrecord(rec no: integer);
begin
if (recno < 0) then
begin
ShowMessage('Cannot Move beyond top of file.');
exit;
end
else if (recno > filesize(moviefile)-1) then
begin
ShowMessage('Cannot move beyond end of file.');
exit;
end;
seek(moviefile, recno);
read(moviefile, movie);
dvdcatform.MovieID.text := movie.Movie_ID;
dvdcatform.MovieName.text := movie.Title;
dvdcatform.MovieFormat.Ite mIndex := movie.Format;
dvdcatform.MovieLocation.T ext := movie.location;
dvdcatform.MovieGenere.Ite mIndex := movie.Genere;
dvdcatform.OtherGenere.Tex t := movie.alt_genere;
dvdcatform.CheckBox1.check ed := movie.deleted;
itemunsaved := false;
if (length(movie.movie_id) < 6) then
NewEntryMenuItemClick(Appl ication);
end;
As far as storing current position in a var, thats fine, but I still need an efficent way to seek a particular record and then overwrite it.
Code for VerifySaveRec and LoadRecord:
procedure TDvdCatForm.VerifySaveRec;
begin
if (itemunsaved) then
if (Application.MessageBox('Y
SaveScreenRecord;
end;
procedure TDvdCatForm.savescreenreco
begin
movie.deleted := dvdcatform.CheckBox1.Check
movie.Movie_ID := dvdcatform.MovieID.text;
movie.Title := dvdcatform.MovieName.Text;
movie.Format := dvdcatform.MovieFormat.Ite
movie.Genere := dvdcatform.MovieGenere.Ite
movie.Location := dvdcatform.MovieLocation.t
if not filepos(moviefile) = filesize(moviefile) then
seek(moviefile, filepos(moviefile));
WRITE(moviefile, movie);
itemunsaved := false;
end;
procedure tDvdCatForm.loadrecord(rec
begin
if (recno < 0) then
begin
ShowMessage('Cannot Move beyond top of file.');
exit;
end
else if (recno > filesize(moviefile)-1) then
begin
ShowMessage('Cannot move beyond end of file.');
exit;
end;
seek(moviefile, recno);
read(moviefile, movie);
dvdcatform.MovieID.text := movie.Movie_ID;
dvdcatform.MovieName.text := movie.Title;
dvdcatform.MovieFormat.Ite
dvdcatform.MovieLocation.T
dvdcatform.MovieGenere.Ite
dvdcatform.OtherGenere.Tex
dvdcatform.CheckBox1.check
itemunsaved := false;
if (length(movie.movie_id) < 6) then
NewEntryMenuItemClick(Appl
end;
your savescreenrecord do already overwrite the next record,
after the load the filecursor is behind the loaded record,
maybe you do duplicate/overwrite records unknown, which may cause the effect the rec#1 is near identical to rec#2
try this change(don't know if this applyable to your logic you use)
if not filepos(moviefile) = filesize(moviefile) then
//seek(moviefile, filepos(moviefile)); this may cause no seek
seek(moviefile, filepos(moviefile)-1);
WRITE(moviefile, movie);
i may also wroing in this case
meikl ;-)
after the load the filecursor is behind the loaded record,
maybe you do duplicate/overwrite records unknown, which may cause the effect the rec#1 is near identical to rec#2
try this change(don't know if this applyable to your logic you use)
if not filepos(moviefile) = filesize(moviefile) then
//seek(moviefile, filepos(moviefile)); this may cause no seek
seek(moviefile, filepos(moviefile)-1);
WRITE(moviefile, movie);
i may also wroing in this case
meikl ;-)
ASKER
Worth a shot. Thanks! I'll try it tonight.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
or you close and reopen your file somewhere