• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 161
  • Last Modified:

fastes reading line by line and deliting readed line

how i can create reading one line by one and readed line delete ???

there are example of my file type and data structure
  TPrograma = record
    task : string[255];
    action : string[30];
    windtext : string[255];

 Programa : TPrograma;
  F : file of TPrograma;

   while not Eof(F) do begin
                Read (F, Programa);

2 Solutions
ZhaawZSoftware DeveloperCommented:
reading one line by one and readed line delete <-- will it result in deleting all entries?
andrezzzAuthor Commented:
delete one line by one. example

read line x
delete line x
read line y
delete line y
ZhaawZSoftware DeveloperCommented:
You can't just delete something from the middle of the file. You'll have to move last record to place where deleted one was and then truncate file (in case if all records/lines are with same size AND if order is not important) OR to rewrite all the file starting with deleted record - it can take a lot of time.

If you need to read and delete more lines/records at the same time, it's better to read them all and then delete them all (not one by one).
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

i have now that i read all and after that i delete. but my program read line and then send it to server. then read next line and send it too.if there will be connection error in middle of the file. how i can know whitch line i must send after connections will be ok.

i think that i send one line by one. and after line is sent, then delete it and go to next line.

is there some way to read file from end ??? then i can use truncate ....
ZhaawZSoftware DeveloperCommented:
Is it some text file (with lines) or some typed files (with records)? In your example there's typed file, not a text file. In this case there's no problems to read the last record, because all records have same size. You may use seek() function to go to last record in this case.
hello andrezzz, i use the TFileStream to read the file into an Array of TProgama and then read the array one member at a time, if the array read is  "Yes" or Good, then I rewrite the file and move all of the records up one postion, and chop off the last record of the file -

procedure TForm1.sbut_ReadProgFileClick(Sender: TObject);
i, size: Integer;
FS1: TFileStream;
aryProg: Array of TPrograma;
FS1 := TFileStream.Create('E:\1 prog.prog', fmOpenReadWrite or fmShareDenyWrite);
  size := FS1.Size;
  if (size = 0) or ((size mod SizeOf(TPrograma)) <> 0) then
    Showmessage('ERROR - This file is NOT correct for this program');
  SetLength(aryProg, size div SizeOf(TPrograma));
  FS1.ReadBuffer(aryProg[0], size);
  //now you have all of the file in memory (aryProg)

  //now you can read the aryProg one record at a time to get your data
  for i := 0 to High(aryProg) do
    if MessageDlg(aryProg[i].task, mtConfirmation, [mbYes, mbNo], 0) = mrYes then
      // I do not have a server so I use a MessageDlg

      // if you click the YES  button on the message dialog -
      // then thefile stream will write over the old data in the file and
      // move the new record data up one record position for a new file
      FS1.Position := 0;
      if i < High(aryProg) then
        FS1.WriteBuffer(aryProg[i+1], SizeOf(TPrograma)*(Length(aryProg)-i));
      // next I will chop off the last record of the old file
      FS1.Size := size - ((i+1)* SizeOf(TPrograma));
      end else Break;

 - - - - - - - - - - - - - - - - - - - - - - - - -

maybe you can get some ideas fron this
ask questions if you need more information

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now