[fpc-pascal]building a command string

Nikolay Nikolov nickysn1983 at netscape.net
Wed Sep 24 20:35:59 CEST 2003


DONALD PEDDER wrote:

>>Exec(GetEnv('COMSPEC'), '/c copy '+ParamStr(1)+' '+ParamStr(1)+'.bak /v');
>>    
>>
>
>   Now THIS is what I needed to know! Thank you. :-) I originally tried a
>writeln kind of syntax - '/c copy ',paramstr(1),' ',paramstr(1),'.bak /v'
>- but that didn't work, hence my other attempts to build the string. I
>didn't know about the syntax you've told me - all I have to do is use a
>plus instead of a comma.
>   What other circumstances would I use this syntax? Is it like a
>back-apostrophe in Unix - evaluate this part before the rest?
>
The plus concatenates strings, the comma separates procedure/function 
arguments. Write/writeln/read/readln accept variable number of arguments 
(due to compiler magic) so you can use the comma with it. But other 
procedures and functions accept only fixed number of arguments, e.g. 
Exec needs exactly 2 strings. (this isn't always the case, due to 
procedure/function overloading and the 'array of const' feature, but 
don't bother with that, for now)

Basically you can always do this:
  Writeln('Hello, ' + 'world!');
  Writeln('Hello, ', 'world!');
and they do the same thing.

Exec wants exactly two strings as parameters. In this case:

Exec(GetEnv('COMSPEC'), '/c copy '+ParamStr(1)+' '+ParamStr(1)+'.bak /v');

the first is

GetEnv('COMSPEC')

the second is

'/c copy '+ParamStr(1)+' '+ParamStr(1)+'.bak /v'

and they're separated by a comma, that's all. Writeln style procedures accept variable number of parameters (i.e. variable number of things separated by commas).

P.S. find a good book about pascal programming and learn how to use strings, they're cool. (And they don't exist in C) :)







More information about the fpc-pascal mailing list