[comp.lang.pascal] initialization of a string

qiming@uhccux.uhcc.hawaii.edu (Qiming Huang) (04/07/90)

Hi, I would like to ask the fellow netters about how I can initialize
a string in Standard Pascal ( Not TP or UCSD Pascal) and write it to 
a nontext file as part of the output. In C, this is pretty easy. But 
I don't know How to do it in Standard Pascal if not using a loop to 
read a string from keyboard.

Thanks very much for your help!

damm@rimfaxe.diku.dk (Kristian Damm Jensen) (04/08/90)

qiming@uhccux.uhcc.hawaii.edu (Qiming Huang) writes:

>... how can I initialize
>a string in Standard Pascal ( Not TP or UCSD Pascal) and write it to 
>a nontext file as part of the output. 

I presume that by string you mean a variable declared as

	type 
	  string : packed array [1..n] of char;
	var
	  foo : string;
	  f : file of string;

This variable is easily initialized using an assignment:

	foo := 'Any string of length n'

but it is necesary (sp?) that the expression is a string of length exacty 
n characters.

To write on a nontext-file you have to declare f as above and write as usual.

Kristian

-------------------------------------------------------------------------
Kristian Damm Jensen (damm@freja.diku.dk)

bigelow@hpfcso.HP.COM (Jim Bigelow) (04/09/90)

To write a string (either a packed array of char or the String data
type) to a file that is not of type text, may require you to use a
variant record.  For instance, writing to a file of bytes can be done
as follows:

  const
   sizeofstring := <n>;
  type
   varRec = packed record 
              case boolean of 
		true: (str : string);
		false: (byte : packed array[1..sizeofstring] of char); 
	      end;
	{ for this to work the variants *MUST* have same size and alignment }
  var
    strng : varRec
    i: integer;
    f : file of char;
  .
  .
  .
  begin
  .
  .
  .
   strng.str :=  'string less than or equal to <n>';
   for i := 1 to sizeofstring do
       write(f,string.byte[i]);
  .
  .
  .
  

Hope this is helpful.

Jim Bigelow
HP9000/S300 Pascal
Colorado Language Lab.
HP
Ft. Collins, CO