1.11M Members

Pascal edit, delete data in text file

 
0
 

Hi everyone, I'm now nearly finished with my project which tells us to write a PASCAL program for entering the information of the employees. However, I came across a BIG problem is that I've got no idea on how to edit records in the text files or delete unwanted data from the text file.
I've thought of:
- copying the data in the text file to an array
- complete the wanted operations in the array
- copy the data in the array back to the text file made blank by 'rewrite'

but I really have NO idea on how to edit or delete records...
the array that is used to store data in the text file now is a 2-D array.
would somebody please please please give me a clue on how to edit, delete data ?? I really need this to pass the term... so, please, please, please...

thanks in advance.

 
0
 

There is in Delphi a type of data TStringList ....
It is very easy to manage a text file using an instance of TStringList ....
But I don't know about Pascal supports TStringList or not ?

 
0
 

um.. thanks both of you for your help. but then, still, I'm unable to find a solution to the problem.. maybe I'm a bit to redundant in defining the problem in my first post here. to simplify that, I've already copied data from text file into a 2-D array..
but I don't know how to edit , delete data in the array.. is there some kind of method?
thanks a lot.

 
0
 

hi :D

program something;
uses crt;
type rec = record
                 line:string;
           end;
     
     trec = array[1..3,1..3]of rec;

var t:trec;
    temp:array[1..9]of rec;
    f:file of rec;
    one:rec;
    i,j:byte;
    s:string;

procedure filefill;
begin
      assign(f,'recs.dat');
      {$I-}
      rewrite(f);
      {$I+}
      if ioresult <> 0 then begin
         writeln('file rewrite error');
         readln;
         halt;
      end;
      writeln('contents of the file: ');
      temp[1].line:='turbo';
      temp[2].line:='pascal';
      temp[3].line:='is';
      temp[4].line:='my';
      temp[5].line:='favorit';
      temp[6].line:='language';
      temp[7].line:='and';
      temp[8].line:='I am a ';
      temp[9].line:='hungarian';

      (*fill the file first*)
      for i:=1 to 9 do begin
          write(f,temp[i]);
          writeln(i,'. element: ',temp[i].line);
      end;
      writeln('file size: ',filesize(f));
      close(f);
      writeln('==========');
end;

procedure readback;
begin
      (*read it back*)
      assign(f,'recs.dat');
      {$I-}
      reset(f);
      {$I+}
      if ioresult <> 0 then begin
         writeln('file reset error');
         readln;
         halt;
      end;

      while not eof(f) do begin
            for i:=1 to 3 do begin
                for j:=1 to 3 do begin
                    read(f,t[i,j]);
                end;
            end;
      end;
      close(f);
end;


procedure writeout(x:trec);
var i,j:byte;
begin
     writeln('==========');
     for i:=1 to 3 do begin
         for j:=1 to 3 do begin
             write('t[',i,',',j,'].element:=',x[i,j].line);
             writeln;
         end;
     end;
end;

procedure modify(x:rec);
begin
     writeln('==========');
     writeln('give me the new string: ');
     readln(x.line);
     t[i,j]:=x;
end;


begin (*main*)

      clrscr;

      filefill;
      readback;
      writeout(t);
      writeln('==========');
      write('give me the first number of the matrix,max 1..3 : ');
      readln(i);
      write('and the second number ,max 1..3 : ');
      readln(j);

      if (i in [1,2,3]) and (j in [1,2,3]) then begin
          writeln('t[',i,',',j,']:=',t[i,j].line);
          modify(one);
          writeln('press enter to read the matrix again');
          readln;
          writeout(t);
      end else writeln('wrong index number entered');

      while not keypressed do continue;
end.
(*created by FlamingClaw 2010.02.17,Hungary*)
 
0
 

omg thank you so much FlamingClaw !
well.. by the way do you have any idea if I enter blank spaces in the program above (indicating I want to delete the data), it can automatically move everything up one line so that the program will not have blank lines in the middle? will I be able to do that?
thanks !!

Question Answered as of 4 Years Ago by FlamingClaw and finalist
 
0
 

FlamingClaw, I have several output files (*.out) contain a lot of information. I would like to have a program can just sort specific data from each output file and save as the same filename with txt extension.

While reading each line (from *.out) if you are on a line that says "state summary:" then save next 10 lines (into *.txt). Please advise. Thanks!

 
0
 

hai.. please anyone help me to solve this code??actually i wanna know how do you update the data into the pascal code. in this case, I want to update data on the movie title. user can update each time the movie title on the latest movie out.below are my pascal coding :

Program RoyalCinema;
uses crt;

type
         movie  = char ;
         AdultRecord  = Record
                        AdmovieTitle   : char;
                        AdmovieSession : movie;
                        AdmovieTime    : real;
                        AdmovieSeat    : movie;
                        AdmovieHall, Adpeople : integer;
                        End;

type    smovie  = char;
        StudentRecord = Record
                        StmovieTitle   : char;
                        StmovieSession : smovie;
                        StmovieTime    : real;
                        StmovieSeat    : smovie;
                        StmovieHall, Stpeople : integer;
                        End;

Var
   AdmovieArray : Array [1..2] of AdultRecord;
   StmovieArray : Array [1..2] of StudentRecord;
   i            : 1..10;
   chooseCust,countA,countB, j, k : integer;
   continue,update                : String;
   total,tot,payAdult,payStud     : real;
   search                         : char;


Procedure displayMENU( );
begin
          writeln('                             WELCOME TO OUR CINEMA                 ');
          writeln;
          readln;
          writeln('*******************************************************************');
          writeln('* HALL  1                                                         *');
          writeln('* [1] Darkest Hour *new         [2] Sumpahan Kum Kum *new         *');
          writeln('*                                                                 *');
          writeln('* HALL  2                                                        *');
          writeln('* [3] Journey 2                 [4] Suatu Malam Kubur Berasap     *');
          writeln('*                                                                 *');
          writeln('* HALL  3                                                         *');
          writeln('* [5] Underworld *new           [6] Hantu Dalam Botol Kicap       *');
          writeln('*                                                                 *');
          writeln('*                                                                 *');
          writeln('* Session Available :                                             *');
          writeln('*                                                                 *');
          writeln('* [1] Day or Night                                                *');
          writeln('* [2] Midnight                                                    *');
          writeln('*                                                                 *');
          writeln('*                                                                 *');
          writeln('* Seat Available :                                                *');
          writeln('*                                                                 *');
          writeln('* [1] Normal Seat                                                 *');
          writeln('* [2] Couple Seat                                                 *');
          writeln('*                                                                 *');
          writeln('*                                                                 *');
          writeln('*******************************************************************');
end;


procedure readCustDetails (var AdCinema : AdultRecord);
begin
          writeln;

          writeln('MOVIE     : ');
          read(AdCinema.AdmovieTitle);
          writeln;
          writeln('SESSION   : ');
          read(AdCinema.AdmovieSession);
          writeln;
          writeln('SHOW TIME : ');
          read(AdCinema.AdmovieTime);
          writeln;
          writeln('CHOOSE THE TYPE OF SEAT');
          read(AdCinema.AdmovieSeat);
          writeln;
          writeln('QUANTITY  : ');
          read(AdCinema.Adpeople);
          writeln;
end;

procedure readStudDetails (var StCinema : StudentRecord);
begin
          writeln;

          writeln('MOVIE     : ');
          read(StCinema.StmovieTitle);
          writeln;
          writeln('SESSION   : ');
          read(StCinema.StmovieSession);
          writeln;
          writeln('SHOW TIME : ');
          read(StCinema.StmovieTime);
          writeln;
          writeln('CHOOSE THE TYPE OF SEAT');
          read(StCinema.StmovieSeat);
          writeln;
          writeln('QUANTITY  : ');
          read(StCinema.Stpeople);
          writeln;
end;


Procedure displayAdult(var AdCinema : AdultRecord);
begin
          writeln('Movie    :: ', AdCinema.AdmovieTitle);
          writeln('Session  :: ', AdCinema.AdmovieSession);
          writeln('Seat     :: ', AdCinema.AdmovieSeat);
          writeln(' ');
end;

Procedure displayStudent(var StCinema : StudentRecord);
begin
          writeln('Movie    :: ', StCinema.StmovieTitle);
          writeln('Session  :: ', StCinema.StmovieSession);
          writeln('Seat     :: ', StCinema.StmovieSeat);
          writeln(' ');
end;


Function ADULTmoviePayment(Adpeople : integer ; AdmovieSeat : movie ; AdmovieSession : movie):real;
begin
         total := 0;

         if AdmovieSession = '1' then
         begin
                if AdmovieSeat = '1' then
                        total := (8.00 + 2.00) * Adpeople
                else
                begin
                        if AdmovieSeat = '2' then
                        total := (8.00 + 4.00) * Adpeople
                end
         end

         else
         begin
                if AdmovieSession = '2' then
                begin
                        if AdmovieSeat = '1' then
                                total := (8.00 + 2.00 + 3.00) * Adpeople
                        else
                        begin
                                if AdmovieSeat = '2' then
                                total := (8.00 + 4.00 + 3.00) * Adpeople
                        {else
                        begin
                                if AdmovieSeat = '3' then
                                total := (8.00 + 3.00) * Adpeople;
                        end;}
                        end;
                end;
         end;
         ADULTmoviePayment := total;
end;

Function STUDmoviePayment(Speople : integer ; SmovieSeat : smovie ; SmovieSession : smovie):real;
begin
         tot := 0;
          if SmovieSession = '1' then
         begin
                if SmovieSeat = '1' then
                        tot := (8.00 + 2.00) * Speople - 0.30
                else
                begin
                        if SmovieSeat = '2' then
                        tot := (8.00 + 4.00) * Speople - 0.30
                end
         end

         else
         begin
                if SmovieSession = '2' then
                begin
                        if SmovieSeat = '1' then
                                tot := (8.00 + 2.00 + 3.00) * Speople - 0.30
                        else
                        begin
                                if SmovieSeat = '2' then
                                tot := (8.00 + 4.00 + 3.00) * Speople - 0.30
                        {else
                        begin
                                if AdmovieSeat = '3' then
                                total := (8.00 + 3.00) * Adpeople;
                        end;}
                        end;
                end;
         end;
         STUDmoviePayment := tot;
end;


Procedure FindAdult(var AdCinema : AdultRecord;  m : char);
begin
        if (AdCinema.AdmovieSession = m) then
           writeln('MOVIE : ',AdCinema.AdmovieTitle);
end;


Procedure FindStud(var StCinema : StudentRecord;  n : char);
begin
        if (StCinema.StmovieSession = n) then
           writeln('MOVIE : ',StCinema.StmovieTitle);
end;

Procedure UpdateAdult(var AdCinema : AdultRecord; s : String);
begin
       if AdCinema.AdmovieTitle = s then
       begin
                AdCinema.AdmovieTitle := ' ';
       end
end;

Procedure UpdateStud(var StCinema : StudentRecord; k : String);
begin
       if StCinema.StmovieTitle = k then
       begin
                StCinema.StmovieTitle := ' ';
       end
end;




begin
     clrscr;

     writeln('*******************     ROYAL CINEMA CUSTOMER SERVICE     *********************');
     writeln;
     displayMENU();

     continue := 'true';
     j := 1;
     k := 1;
     countA := 0;
     countB := 0;
     if continue = 'yes' then
     begin

        writeln;
        writeln('PLEASE CHOOSE THE TYPE OF CUSTOMER BASED ON THESE FOLLOWING CODE : ');
        writeln;
        writeln(' [1] Normal               [2] Student     ');
        writeln;
        writeln('--------------------------------------------------------------------');
        writeln;
        readln(chooseCust);
        if chooseCust = 1 then
                begin
                        {countA := countA + 1;}
                        for i := 1 to 2 do
                        begin
                                readCustDetails(AdmovieArray[countA]);
                        end;
                end
        else if chooseCust = 2 then
                begin
                        {countB := countB + 1;}
                        for i := 1 to 2 do
                        begin
                                readStudDetails(StmovieArray[countB]);
                        end;
                end;
        writeln;
        readln();
     end;

     clrscr;
     writeln('LIST OF NORMAL CUSTOMER : ');
     writeln('--------------------------------------------------------------------');
     writeln('');
     for i := 1 to 2 do
     begin
        displayAdult(AdmovieArray[i]);
        {payAdult := ADULTmoviePayment(AdmovieArray[i]);
        writeln('The Total Payment (RM) : ',payAdult:2:2);}
     end;
     writeln;

     clrscr;
     writeln('LIST OF STUDENT CUSTOMER : ');
     writeln('--------------------------------------------------------------------');
     writeln('');
     for i := 1 to 2 do
     begin
        displayStudent(StmovieArray[i]);
        {payStud := STUDmoviePayment(StmovieArray[i]);

        writeln('The Total Payment (RM) : ',payStud:2:2);}
     end;
     writeln;


     clrscr;
     writeln;
     writeln('PLEASE ENTER THE SESSION THAT YOU WANT TO SEARCH : ');
     readln(search);
     writeln('--------------------------------------------------------------------');

     for i := 1 to 2 do
     begin
        writeln;
        FindAdult(AdmovieArray[i],search);
        readln;
        writeln;
        writeln;
        FindStud(StmovieArray[i],search);
        readln;
     end;


     clrscr;
     writeln;
     writeln('PLEASE ENTER THE MOVIE TITLE THAT YOU WANT TO UPDATE : ');
     readln(update);
     writeln('--------------------------------------------------------------------');

     for i := 1 to 2 do
     begin
        writeln;
        UpdateAdult(AdmovieArray[i],update);
        readln;
        writeln;
        writeln;
        UpdateStud(StmovieArray[i],update);
        readln;
     end;
     writeln('Movie has been succesfully updated');

     writeln;
     writeln('Do you wish to continue? (yes/no)');
     readln(continue);
end.
 
0
 

Start a new thread for this, instead of hijacking an old one, and please use code tags. Do not forget to post your error too.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: