fastes reading line by line and deliting readed line

Posted on 2005-05-14
Last Modified: 2010-04-05
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);

Question by:andrezzz
    LVL 11

    Expert Comment

    reading one line by one and readed line delete <-- will it result in deleting all entries?

    Author Comment

    delete one line by one. example

    read line x
    delete line x
    read line y
    delete line y
    LVL 11

    Expert Comment

    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).

    Expert Comment

    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 ....
    LVL 11

    Accepted Solution

    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.
    LVL 33

    Assisted Solution

    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 run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
    Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
    This video discusses moving either the default database or any database to a new volume.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    731 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

    17 Experts available now in Live!

    Get 1:1 Help Now