Hello

I'm making a program where the user will be able to organise their orders so they see which ones are complete and incomplete.

I want to allow the user to be able to delete an entry from a file (*.txt) as this will be used in two ways:
1. To remove an accidental submission from IncompleteOrder File.
2. To remove an entry from the IncompleteOrder File once it has been added to the CompleteOrder file.

I know how I will go about getting the information from the IncompleteOrder file and writing it to the CompleteOrder file, I just need to know if there is a way of deleting an entry from a file.

Thanks

Lewis

Recommended Answers

All 3 Replies

what if you copy the contents of your text file into an array
if your condition becomes true then delete one element from that array and write back to the file .....
I wrote a console apps for this problem.
Your text file located in 'C:\' and named 'yourfile.txt' by me
write these sample words like
apple 'press enter
wine
green
day

Program text_into_array;

{$APPTYPE CONSOLE}

Uses
  SysUtils;

Var F:Text;
    S,FileName:String;
    Lines,temp:Array[1..10]Of String; {max ten lines}
    i,j,k,l:Integer;

Begin{main}
  FileName:='C:\YourFile.txt';
  i:=0;
  AssignFile(F,FileName);
  Reset(F);
  WriteLn('The contents of the ',FileName);
  While EoF(F)<>True Do Begin
    Inc(i);
    ReadLn(F,S);
    Lines[i]:=S;
    WriteLn(i,'. ',Lines[i]);
  End;
  CloseFile(F);
  WriteLn;
  WriteLn('The contents of the array:');
  For j:=1 To i Do Begin
    WriteLn('Lines[',j,']:= ',Lines[j]);
  End;
  WriteLn;
  WriteLn('Let''s delete the 3. element');
  Lines[3]:='';
  For j:=1 To i Do WriteLn('Lines[',j,']:= ',Lines[j]);
  WriteLn;
  WriteLn('Ok,now let''s copy the not empty elements to another array');
  k:=i;
  j:=1;
  l:=1;
  While k>0 Do Begin
     If Lines[j]<>'' Then Begin
      Temp[l]:=Lines[j];
      Dec(k);
      Inc(j);
      Inc(l);{only if not empty}
     End
     Else Inc(j);
  End;
  WriteLn;
  WriteLn('Ok,now rewrite the old file');
  Rewrite(F);
  For j:=1 To i-1 Do Begin
    WriteLn('temp[',j,']:= ',temp[j]);
    WriteLn(F,temp[j]);
  End;
  Close(F);
  WriteLn;
  WriteLn('The modified text file again:');
  Reset(F);
  While Not EoF(F) Do Begin
    ReadLn(F,S);
    WriteLn(S);
  End;
  ReadLn;
End.

{
-=Created By FlamingClaw 2009.06.14=-
}

I tried using an array but it would only retrieve the last bit of information from it. I then tried a different variation which still didn't work.
I have solved the problem though. I loop through the file and it will copy the information from the OrderFile to the TempOrderFile. If the OrderNumber is equal to the one selected though, it will not copy it. It then clears all the information in the OrderFile and then just copies the information back over from the TempOrderFile to the OrderFile.
So, although arrays weren't a success, thanks again for the help FlamingClaw =]

next time you can try to read the file into linked-list,the list's elements will the line of the file,so you have completed.txt and a incompleted.txt and you read first the incomplete file into linked list
and then read the completed.txt and check if are they same if same then the list's element is set to ' '.Rewrite the incompleted.txt and write only those elements that length is more than 0

Program Solution;

Type Tr = ^r;
     R = Record
               Data:string;
               Address:tr
         End;

Const
     Cp='c:\completed.Txt';
     Ip='c:\incompleted.Txt';

Var
   Completed,Incompleted:text;
   S:string;
   I:byte;
   Els,Uj,Akt:tr;

//reader
Procedure Reading(W:string[50];var X:text;y:string;z:byte);
Begin
     Writeln(W);
     Reset(X);
     If Eof(X)=true Then Writeln('The File Is Empty!');
     While Not Eof(X) Do Begin
           Readln(X,Y);
           Writeln(Z,'.',Y);
           Inc(Z);
     End;
     Writeln;
End;

//lister
Procedure Linkedlister(Var X:text;paths,Line:string);
Begin
     Reset(X);
     Writeln('Result Of List From : ',Paths);
     If (Eof(X)= True)then Begin
        Writeln('The File ',Paths,' Is Empty,Can''t Create List');
        Readln;
     End;
     Els:=nil;
     Repeat
           Readln(X,Line);
           New(Uj);
           Uj^.Data:=line;
           Uj^.Address:=nil;
           If (Els=nil)then Els:=uj
           Else Akt^.Address:=uj;
           Akt:=uj;
     Until Eof(X)=true;
     Akt:=els;
     While Akt <> Nil Do Begin
           Writeln(Akt^.Data);
           Akt:=akt^.Address;
     End;
     Writeln;
End;

//file And List Checker
Procedure Checker(Var X:text);
Begin
     Writeln('File And List Checker');
     Reset(Completed);

     While (Eof(Completed)=false)do Begin
           Readln(Completed,S);
           Writeln(S);
           Akt:=els;
           While Akt <> Nil Do Begin
                 If (S = Akt^.Data) Then Akt^.Data:='';
                 Akt:=akt^.Address;
           End;
     End;
     Writeln('write back only those that length isn''t zero');
     Rewrite(Incompleted);
     Akt:=els;
     While Akt <> Nil Do Begin
           If (Length(Akt^.Data)>0) Then Writeln(Akt^.Data);
           Writeln(Incompleted,Akt^.Data);
           Akt:=akt^.Address;
     End;
     Writeln;
End;
//the Main
Begin
   Assign(Completed,Cp);
   Assign(Incompleted,Ip);
   S:='';
   I:=1;
   Reading('The Contents Of '+Ip,Incompleted,S,I);
   Reading('The Contents Of '+Cp,Completed,S,I);
   Linkedlister(Incompleted,Ip,S);
   Checker(Completed);
   Close(Incompleted);
   Close(Completed);
   Readln;
End.
{Created by FlamingClaw 2009.06.19}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.