Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting records in typed files in Turbo Pascal

Status
Not open for further replies.

Actor

Programmer
Nov 19, 2003
20
0
0
US
Is there any way to delete a record in a typed file? For example:
Code:
type
   whatype = file of string ;

var
   f : whatype ;
   i : longint ;

begin
   i := 99 ;
   assign (f, 'whatever') ;
   reset (f) ;
   delete (f,i) ;
   close (f)
end.

In the above code delete (var f:whatype ; i:longint) would remove the ith record of file f and reduce the file size by one. Does such a function exist?

Hopefully such a function would delete any record but all I really need is to delete the last record.

If I have to write the function myself the only strategy that comes to mind is to copy the entire file, sans ith record, to a second file, then delete the first file and rename the second file. Is there a more elegant strategy?

Thanks in advance for any assistance. I am traveling and will probably not be able to check any responses before 16Jul06.

 
No, there is not such a function. As far as deleting records in typed files, there's two ways to do it:

1) As you describe, copy the entire file, sans ith record, to another file, delete the original, and rename the new file back.

Or the elegant way:
2) Maintain a deleted marker in each record. All that would be necessary there is a boolean true if deleted. Then in your code, you would set the other functions to ignore those deleted records (unless you wanted an undelete function).

Then, of course, if you get enough deletions, you would want to perform #1 on the file, ignoring all deleted=true flags.
 
Thanks for the input Glenn9999. That's exactly what I thought but it appears that we both may be wrong.

Just yesterday I was browsing through Turbo Pascal 7 the Complete Reference by Stephen K. O'Brien when I found this:

Truncate

procedure Truncate(var F);

Forces end of file to be the current position of the file ointer. Contents of the file beyond the file pointer are lost.


I have not had the time to try it yet but this appears to be exactly what I need since I only need to delete the last record. The description seems to indicate that it will work on text files as well.

In the program I am writing I have a variable Last_Rec which keeps track of where the last record is. Anything beyond Last_Rec is garbage. If I can delete the last record then I can turn Last_Rec into a function where

Last_Rec := FileSize(f) - 1.

This seems like a safer approach.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top