Hi

I want to read a pascal file a find a certain word in that file.

Thanks

Recommended Answers

All 8 Replies

> Hi
Hi.

> I want to read a pascal file a find a certain word in that file.
Many people would like to do that.

> Thanks
You're welcome.

Link. (Show me what you've done to solve your problem and I'll show you some help.)

Hi

well the thing is that i have designed the next program. It is the catalogue of a library. By the time, you can introduce the title of the book you are searching for, but i would like to go farther. My inttention is that if i introduce the letter 'n' to find all the books that contain that the letter in the title. Or, more simply, if i introduce the word in to find al the books that contain in the title that word.

program pro;
{$APPTYPE CONSOLE}


uses SysUtils;

type
   tbook = record
      title : string[100];
      author : string[100];
      date : integer;
      number: integer;
      information: string[255];
   end;


   procedure searchtitle(book: array of tbook);

   var
   fbook : file of tbook;
   title1:string[20];
   I,contador,opcion1,opcion2,opcion3:integer;
   infor,opcion4,repetir:char;

   begin


    assign (fbook, 'c:\dos.txt');

   rewrite(fbook);

      with book[1] do begin
         title :='storm in new york';
         author :='ken follet';
         date :=1998;
         number:=123;
         information:='Magnific book';
      end;

       with book[2] do begin
         title :='in the town';
         author :='ken smith';
         date :=1994;
         number:=124;
         information:='extraordinary nobel';
      end;

      with book[3] do begin
          title :='in ireland';
         author :='oscar wilde';
         date :=1999;
         number:=125;
         information:='Do not loose it';
      end;

   for I:= 1 to 3 do
      write(fbook, book[I]);

   I := 1;
   contador:=0;
   repetir:='0';

   repeat
   reset(fbook);
   write('    Write the title of the book you want to find ');
   readln(title1);
   writeln(' ');
   writeln('     Search results:');

   while not EOF(fbook) do begin
      read(fbook, book[I]);
      if book[i].title=title1 then begin
      writeln(' ');
      writeln ('        Book ',I, ' : ' , book[I].title, ' ', book[I].author, ' ', book[I].date);
      writeln(' ');
      contador:=contador+1;
      I := I + 1;
      end;
      end;
      if (contador=0) then begin
      writeln(' ');
      writeln('      We don´t have the book you are searching for. Do you want to repeat the searching? ');
       writeln(' ');
       writeln('        choose 1 to afirm');
       writeln(' ');
       writeln('        choose 2 to deny');
       writeln(' ');
       readln (repetir);
       writeln(' ');
      end;
   until (repetir='2') or (contador>0);

   writeln(' ');
   writeln('   Do you want information about any of the books? ');
   writeln(' ');
   writeln('      choose 1 to afirm');
   writeln(' ');
   writeln('      choose 2 to deny');
   writeln(' ');
   readln(opcion4);
   writeln(' ');

   if opcion4='1' then begin
   repeat
   write('     you need information of, how many books? ');
   readln(infor);


   case infor of

   '1': begin
        writeln(' ');
        write('       Which of the books searched for does interest you? ');
        readln(opcion1);
        writeln(' ');
        writeln('        ',book[opcion1].title,' ',book[opcion1].information);
        end;

   '2': begin
        writeln(' ');
        write('      Which of the books searched for does interest you? ');
        readln(opcion1,opcion2);
        writeln(' ');
        writeln('        ',book[opcion1].title,' ',book[opcion1].information);
        writeln(' ');
        writeln('        ',book[opcion2].title,' ',book[opcion2].information);
        end;

   '3': begin
        writeln(' ');
        write('      Which of the books searched for does interest you? ');
        readln(opcion1,opcion2,opcion3);
        writeln(' ');
        writeln('        ',book[opcion1].title,' ',book[opcion1].information);
        writeln(' ');
        writeln('        ',book[opcion2].title,' ',book[opcion2].information);
        writeln(' ');
        writeln('        ',book[opcion3].title,' ',book[opcion3].information);
        end;
   end;

   writeln(' ');
   writeln(' ');
   writeln('     Do you want to search for more information?');
       writeln(' ');
       writeln('        choose 1 to afirm');
       writeln(' ');
       writeln('        choose 2 to deny');
       readln (repetir);
   until (repetir='2');
   end;

   close (fbook);
 end;


 var
   book : array[1..5] of tbook;
   opcion: char;

 begin

 repeat
 writeln(' ');
 writeln(' ');
 writeln('  -----------------------------------------------');
 writeln(' ');
 writeln('      HOUSTON LIBRARY');
 writeln(' ');
 writeln(' ');
 writeln('      Welcome to our catalogue.');
 writeln(' ');
 writeln('      1.-Search by title');
 writeln(' ');
 writeln('      2.-Search by author');
 writeln(' ');
 writeln('  -----------------------------------------------');
 writeln(' ');
 write('      Choose the option you want: ');
 readln(opcion);
 writeln(' ');
 writeln(' ');

  case opcion of
        '1': begin
             searchtitle(book);
             end;
  end;
  until (opcion<'1')or (opcion>'2');
  readln;
end.

Thanks in advance.

Ah, this is an interesting problem. I have to go somewhere right now, but I'll give you some attention later today.

Is this schoolwork? Or is it business? (I'll make you work harder if its for business, since commercial programs must be much more robust than school programs...)

Hasta luego.

Hi

It isn't schoolwork nor bussineswork. It is an university project.

Bye.

A university is a type of school, so it is schoolwork.

While your code works, I think you are trying to do too many things at once. For example, the procedure searchTitle should not create the file also, even just for testing. Create a procedure that creates/writes the file from the book array (say, ArchivarLibros), and another that reads it into the book array (say, LeerLibrosDeArchivo). Then, if you want to just make a file for testing, create a procedure that is called first which fills the array and saves it to file with the ArchivarLibros procedure.

You will also want to use a dynamic array instead of a fixed array. Dynamic arrays in Delphi always begin at zero, not one.

Whenever you find that you are doing something over and over, that is a good hint that a small function or procedure might be handy. For example:
[[I][/I]code=Delphi[I][/I]]

function PedirConfirmarONegar( msg: string ): boolean;
  var
    respuesta: string;
  begin
  { La falla es fracasar, para un compiler feliz }
  result := false;

  { Pida el mensaje }
  writeln;
  writeln( msg );
  write( ': ' );

  { Repita hasta una respuesta correcta }
  repeat
    readln( respuesta );
    case upCase( respuesta[ 1 ] ) of
      'Y': begin
           result := true;
           break
           end;
      'N': break
      else begin
           writeln( 'Please answer YES or NO' );
           write( ': ' )
           end
      end;
  until false
  end;

[[I][/I]/code[I][/I]]

OK, now for your actual question. You want to search titles for substrings. In Delphi, use the AnsiPos function to determine whether a substring is found in a string.

if ansiPos( upCase( stringABuscar ), upCase( stringAEscudrinar ) ) <> 0
  then writeln( 'found it' )
  else writeln( 'could not find it' );

For school you may get away with such a simple searching method, but in real life you would have to account for misspelled words and words with multiple spellings.

Hope this helps.


Hi

well the thing is that i have designed the next program. It is the catalogue of a library. By the time, you can introduce the title of the book you are searching for, but i would like to go farther. My inttention is that if i introduce the letter 'n' to find all the books that contain that the letter in the title. Or, more simply, if i introduce the word in to find al the books that contain in the title that word.

Thanks in advance.

Hi Douas

Excuse for writing the previous post. It was an error.

The thing is that your solution gives me the next problem:

Incompatible types Char and ShortString.


Thanks.

The Pos and AnsiPos functions require string arguments. Not char. Make sure you are using strings.

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.