Searching a text file with pascal

johnroach1985 0 Tallied Votes 539 Views Share

This program searches the given txt file.And tells you how many there are. Just a reminder don't forget to put the searced txt file in C: or change it accordingly for more info or questions Silver.Lost.Raven@gmail.com have fun!!

{Full-Name:Lost-Raven}
{A MENU DRIVEN PASCAL PROGRAM to find the sequencing char array which could be
found in the rand.txt file which is in c:\ folder.For this program I have used
arrays while loops if's repeat until's and case's everything is written in the
program if its not working or if it gives a runtime error please check help(read
known bugs).If you want more info on the program contact me by
 If you want more info on me visit my website
www.johnroach.edu.tc thanks for using my program.... }
{
----------->Known bugs in this program:

1-In some compilers maxint is not understood.we can write 13000 intead of it.I
written it thinking it will work in your compiler.

2-This program has been written with dev-pascal 1.9.2 so there may be some
compiler issues i am not aware of.(I have tested this program in freepascal and
in bloodshed dev-pascal worked smoothly in those).This program gives no syntax
errors in the compilers above.

3-Some writing in the output tend to go to the next sentence this is mostly
caused of using small dos windows  please use the full screen mode for better results.

}

Program HW3 (fdata,Output,Input);


var
   i,t,r,c,z,m:integer;
   fdata : TEXT;
   a : array[1..maxint] of char;
   b:array[1..1000] of char;
   q,x,n,s:char;{the checking of the menu}
   sentinel:array[1..4] of boolean;
   choice:char;

procedure datatoarray;{as you may have understand this throws all of the rand file
into a array}

        begin
             z:=1;
             repeat
                   read(fdata,a[z]);

                   z:=z+1;
             until(EOln(fdata)) ;

        end;
Procedure about;{Who wrote it?Why?Is there more i can see?}

          begin
               writeln('  ');
               writeln(' About  ');
               writeln('--------');
               writeln('  ');
               writeln(' This program has been written by John Roach');
               writeln('  ');
               writeln(' It is a program that can find the wanted sequence in the  ');
               writeln('  ');
               writeln(' file rand.');
               writeln('  ');
               writeln(' For any questions comments or extra points please contact me');
               writeln('  ');
               writeln(' e-mail:Silver.Lost.Raven@gmail.com');
               writeln('  ');
               writeln(' Also for daily updates on Johns life please visit');
               writeln('  ');
               writeln(' http://www.johnroach.edu.tc    ');
               writeln('  ');
               writeln('  !!!P.S!!!!THERE ARE NEW UPDATES ON MY SITE!!!');
          end;
Procedure help;{Every program needs a help text }
          begin
            writeln('-');
            writeln(' ');
            writeln(' HELP');
            writeln('------');
            writeln('  ');
            writeln(' The Reasons Why Didn"t the program work');
            writeln('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
            writeln('  ');
            writeln('1-)You may have entered too many variables to find');
            writeln('  ');
            writeln('Please notice that the max variables is 1000.');
            writeln('  ');
            writeln('2-)Your rand.txt file is not in C:\rand.txt please put it there');
            writeln('  ');
            writeln('3-)You may have forgotten to put a $ sign right after the last');
            writeln('  ');
            writeln('char you have entered.This mistake causes a runtime error.');
            writeln('  ');
            writeln('4-)Don"t forget you have to go step by step!!!!!!');
            writeln('  ');
            writeln('5-)Even tough this program is to be said to be bugless');
            writeln('  ');
            writeln('i may have missed something.(Its not likely)');
            writeln('  ');
          end;
procedure menu;{standart issue menu}
           begin
             writeln('-');
             writeln(' ');
             writeln(' MENU');
             writeln('------');
             writeln('Press 1 to enter the sequence you are looking for       ',n,s,q,x);
             writeln('Press 2 to see if we can find your sequence ');
             writeln('Press 3 to go to Help');
             writeln('Press 4 to go to About');
             writeln('Press 0 to EXIT');
             writeln(' ');
           end;
procedure userinput;{geting the user input and defining it as an char array}
             begin
                  i:=1;
                  writeln('-');
                  writeln(' ');
                  writeln('Please enter the sequence you want to find. ');
                  writeln('And remember not to exceed more than 1000 characters. ');
                  writeln('If you have problems with the program please consult  ');
                  writeln('the help menu.');
                  writeln('After you have finished inputing the sequence press $ then enter ');
                  m:=1;
                  repeat{repeats until end of line}
                        read(b[i]);
                        i:=i+1;
                  until(eoln) ;
                  if (b[m]=b[2]) then
                                           begin
                                           sentinel[3]:=true;
                                           sentinel[4]:=true;
                                           end;
                        if (b[m]<>b[2]) then
                                           begin
                                           sentinel[2]:=false;
                                           sentinel[1]:=false;
                                           end;{ok its hard to
                        explain why i did this but i will try.this is for a
                        special condition for ele108 and such cases.because there is an extra e
                        in front one of them such as eele108 this gives a wrong
                        answer so i had to put a check on -procedure find- but later on
                        I tought what if the man inputs a double sequence such as
                        eele108 what if this is what the user is trying to find so this
                        that check}
                  if i>1000 then {to stop warn people of the overflow!!!}
                                begin
                                     writeln(' ');
                                     writeln('!!!!WARNING!!!!You have entered more than 1000 chars');
                                     writeln('My program was designed for a sequence shorter than');
                                     writeln('or equal to 1000 chars');
                                     writeln(' ');
                                end; {end of if}
                  writeln(' ');
                  writeln(' ');
          end;{end of process userinput}

procedure find;{this is where the GOLD lies.This where we find the user inputed sequence!!!}
          begin
               m:=0;
               c:=-1;{because the while loop will increment the c the first time}
               r:=1;
               while r<>z do{z as you can see above z is the last index for the array a[] used}
                        begin

                     repeat
                           if b[t]=a[r] then  {a is from text file and b is from input user}
                              begin           {increments both at the same time to check the next char}
                                   t:=t+1;
                                   r:=r+1;
                                   m:=t-1;
                                   if ((b[m]=a[r]) and   (sentinel[1]=false) and (sentinel[2]=false) and (sentinel[3]=false) and (sentinel[4]=false) ) then begin c:=c+1;end;
                                   {I have explained why I have written this in -procedure userinput- this is the check
                                   that I was talking about}
                              end
                            else
                                begin
                                  t:=1;        {if all of the b array wasn't equal till end of b we will have to re-check}
                                  r:=r+1;
                                 sentinel[1]:=false;{Again this is for the use of checking that i have explained in
                                 -procedure userinput-}

                                end; {end of if}


                     until ((b[t]='$') or (r=z));
                     c:=c+1;

               end;{end of while}

          end;{end of procedure}

procedure dochoice;{the procedure that brings out the menu}

          begin
              case choice of

                 '1': begin
                           userinput;
                           n:='-';{well actually made this one for fun :-) it writes ok next to the menu}
                           s:='>';
                           q:='O';
                           x:='K';
                           menu;
                      end;

                 '2':begin

                          find;
                          writeln('This is how many of your sequence is in the rand.txt file ' ,c);
                          If c=0 then writeln('So basicly you have used the program wrong or there is no such sequencing char.') ;

                           n:=' ';{just to clean up the menu}
                           s:=' ';
                           q:=' ';
                           x:=' ';
                          menu;

                     end;
                 '3':begin
                   help;
                   menu;
                   end;
                 '4':begin
                  about;
                  menu;
                  end;
                end;{end of case statement}








          end; {end of dochoice}
BEGIN


assign( fdata, 'C:\rand.txt' );{this is where you specify the address}
reset(fdata );


datatoarray;

n:=' ';
s:=' ';
q:=' ';
x:=' ';
menu;

repeat
readln(choice);
dochoice;
until(choice='0');


CLOSE( fdata );



END. {end of program}