TIP: Click on subject to list as thread! ANSI
echo: z3_pascal
to: Tim Chapman
from: Bob Lawrence
date: 1996-11-17 08:42:16
subject: pointers

TC> Type
 TC> tim = String [80];

 TC> Var
 TC> file_info: Array [1..200] Of ^tim;
 TC> code: Array [33..127] Of Char;
 TC> X: Integer;
 TC> number: Integer;
 TC> file_name: String [8];
 TC> a: Text;
 TC> letter: Char;

 TC> Assign (a, 'c:\ezy\doors\gods\tim.tim');
 TC> Reset (a);
 TC> X := 1;
 TC> While Not EoF (a) Do
 TC> Begin
 TC> New (file_info [X] );
 TC> ReadLn (a, file_info [X]^);
 TC>                              {I want to use the seperate letter 
 TC> X := 1 + X;              {here in the code to encrypt the  file}
 TC> End;
 TC> Close (a);
 TC> End;

  No worries. You've allocated memory to the tim[80] strings using
new() and that memory is pointed at by the pointers in file_info[].
This will work. ReadLn() will read a line up to the carriage return
and WriteLn() will write the line with a carriage return cr/lf added
on the end. 

  The string is file_info[X]^         and the 10th character is 
                file_info[X]^[10]
   
  From there on, you just run along each string and change the active 
characters one at a time, from 1 to Length(string)...

  The replacement character is accessed from your code[] array using
the ord(char) number

  for n := 1 to Length(file_info[X]^) do 
    file_info[X]^[n] := code[ord(file_info[X]^[n])];

  And don't forget to free all the memory at the end, or it'll eat the
RAM and never give it back.

  for n := 1 to (X - 1) do Dispose(file_info[n]);

  When you do this, you should realise that your lovely pointer array
is not actually used. You only use one line at a time, so you could
make do with a single string and read/write in the one procedure. Just
read a line to the string, change the characters in the string, and
write it to the output file. You might add an "IF" so you only change 
the characters if they are ascii in the range 33..127 

var
  s:              string
  code:           array[33..127] of char;
  Output, Input:  Text;
  n, x:           integer;

begin
  Assign(input, 'whatever');
  Reset(input);
{you should do an error handler here, in case the file is missing}  
  Assign(output, 'whatever\else');
  Rewrite(output);

  while not EOF(input) do begin
    readln(Input, s);                       {read a line}
    for n := 1 to length(s) do 
      if ord(s[n]) in [33..127] then s[n] := code[ord(s[n])];
    writeln(Output, s);                     {write the revised line}
  end;
  close(input); close(output);
end;

  You're not limited to 200 lines now...

 TC> Procedure code_make;
 TC> Begin
 TC>    Randomize;
 TC>    number := Random (100);
 TC>    For X := 127 downto 33 Do
 TC>    Begin
 TC>      code [X] := Chr (160 - x);
 TC>      writeln(code[x]);
 TC>      readln;
 TC>    End;
 TC> End;

  This is a pretty awful encryption (reversing the numbers and leaving
the spaces). Here is a way to make a random 95-character set...

var
  code:     array[33..127] of char;
  picked:   array[33..127] of boolean;
  num:      byte;
begin
  randomize;
  for n := 33 to 127 do begin       {95 character set}
    repeat
      num := 33 + random(95)
    until picked[num] = false;      {find an unused number in the set}
    picked[num] := true;            {mark the number picked} 
    code[n] := chr(num);            {put it in the code array}  
  end;
end;

  The picked[] array keeps track of the numbers already picked by the
random number generator to create 95 different numbers in the range 
33-127.

Regards,
Bob

___ Blue Wave/QWK v2.12
@EOT:

---
* Origin: Precision Nonsense, Sydney (3:711/934.12)
SEEN-BY: 633/267 270
@PATH: 711/934 808 50/99 635/728 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.