Solved

Remove record from tfilestream

Posted on 2000-02-24
9
251 Views
Last Modified: 2010-04-04
How to do that?

Imagine I have 30 records and I wish to remove the 4th

Another thing... Is there any quicksort function for that? If possible, tell me how to use it with the file stream... (I'll give extra 50 points)

thanks
0
Comment
Question by:jpcs
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 5

Expert Comment

by:TheNeil
ID: 2557071
I don't think you can do it directly (I'm guessing here) but what about running through your data and copy the records that you need into a second file stream?

The Neil
0
 
LVL 10

Accepted Solution

by:
Lischke earned 50 total points
ID: 2557123
Simple answers are: no and no.

More complete answers are:

1) In order to remove a record from a file you would need to move up the part of the file which is behind a specific position. This is not supported by the OS so you have first to make a memory representation of the file (e.b. by writing the records you wanna keep into a memory stream) and then overwrite the existing file with this memory representation.

2) As the physical backup of a file stream is the file itself you cannot directly sort (or move, for that matter) a part. You also have to make a memory representation, sort this and write it back.

The professional answer is you would need in both cases to do an external merge sort or replacment sort which are algortithms described in "Algorithm in C++" written by Robert Sedgewick (ISBN 3-89319-462-2). Sorry, it would be too expensive to describe them here, but the book is really good (even if the samples are written in C++).

Ciao, Mike
0
 
LVL 1

Author Comment

by:jpcs
ID: 2558940
Thanks for the comments.

My idea is to do as I used to before...

for i := record_pos to filesize div sizeof(filedata) do
begin
   read the next record;
   go back one position;
   write next record on curr pos;
end;
truncate last record.


It worked, and I'm still trying to make it, but I'm having some problems since it's the first time I'm using streamfiles


thanks
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2559224
Well, what you are actually doing is the naive approach to the external merging I mentioned in my last comment. For the case where you just want to remove a record I suggest that you have a closer look at TStream.CopyFrom.

What you actually can use is two have two streams (source and target). You need to open both and copy from the soure:

  TargetStream.CopyFrom(SourceStream, NumberBytesToFirstRecordToSkip);

where the position of both streams is at start of course 0. Now set

  SourceStream.Position := BytePositionAfterRecordsToSkip;

and finally:
  Target.CopyFrom(SourceStream, AnyLargeNumberOrLessForMoreToSkip);

This way, you can avoid touching each and every single record, but can move large parts, which results in faster operation.

Ciao, Mike
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:Lischke
ID: 2559233
Forgot to mention: The disadvantage is obvious. You need a temporary file to hold the target stuff. On the other hand this approach takes the burden from the memory to hold potentially large data blocks.

Ciao, Mike
0
 

Expert Comment

by:arcrotty
ID: 2598951
my 2 cents...

Open old file

rename old file to *.bak

create new file

copy data in one large chunk to the position of the file you want to remove.

Advance the position of the old file by the length of the record you want to remove.

Copy the remaining data from old to the new file.

close files

delete *.bak file

0
 

Expert Comment

by:arcrotty
ID: 2598953
null
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2599603
That's exactly the same principle I suggested. The only difference is that you directly work with files, while I suggested streams. But otherwise... the same idea.

jpcs,

will you now accept and grade a comment or are you still looking for another solution? Some reaction from you would be great. Keep in mind, WE are solving YOUR problem, not vice versa.

Ciao, Mike
0
 
LVL 1

Author Comment

by:jpcs
ID: 2601874
Well I've already solved my problem with the way I suggested... I was doing all right but with a little problem (sizeof(record)+1) instead of (sizeof (record))+1... so it was adding one to the size and not working well...

This may not the the best way, but has the file will not have many record, it's ok


Sorry for the delay, but as I had solved my problem a long time ago, I forgot to accept the comment. Today, the emails remembered me.


regards
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to convert wav to mp3 in delphi 9 174
Newbie Thread Programming 1 139
Working with hours 3 46
Why is there a Difference between the SQL String and using the Parameter ? 7 31
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now