Link to home
Start Free TrialLog in
Avatar of DanEgli
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.NextRecBtnClick(Sender: TObject);
var thefilepos : integer; // for debugging
begin
VerifySaveRec;
TheFilePos := filepos(moviefile); // for debugging
LoadRecord(filepos(moviefile)+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!
Avatar of kretzschmar
kretzschmar
Flag of Germany image

you may seeking somewhere to another pos,
or you close and reopen your file somewhere
maybe you should use a var for storing your current pos,
instead of seeking relative
Avatar of rondi
rondi

Hi,

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.
Avatar of DanEgli

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('You have not saved this entry. Do you wish to save it now?', 'Exit Confirmation', MB_YESNO+MB_ICONQUESTION) = 6) then
    SaveScreenRecord;
end;


procedure TDvdCatForm.savescreenrecord;
begin
movie.deleted := dvdcatform.CheckBox1.Checked;
movie.Movie_ID := dvdcatform.MovieID.text;
movie.Title := dvdcatform.MovieName.Text;
movie.Format := dvdcatform.MovieFormat.ItemIndex;
movie.Genere := dvdcatform.MovieGenere.ItemIndex;
movie.Location := dvdcatform.MovieLocation.text;
if not filepos(moviefile) = filesize(moviefile) then
seek(moviefile, filepos(moviefile));
WRITE(moviefile, movie);
itemunsaved := false;
end;

procedure tDvdCatForm.loadrecord(recno: 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.ItemIndex := movie.Format;
  dvdcatform.MovieLocation.Text := movie.location;
  dvdcatform.MovieGenere.ItemIndex := movie.Genere;
  dvdcatform.OtherGenere.Text := movie.alt_genere;
  dvdcatform.CheckBox1.checked := movie.deleted;
  itemunsaved := false;
  if (length(movie.movie_id) < 6) then
  NewEntryMenuItemClick(Application);
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 ;-)
Avatar of DanEgli

ASKER

Worth a shot. Thanks! I'll try it tonight.
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial