[fpc-pascal] Adding file to string to the RTL

Santiago A. svaa at ciberpiula.net
Fri Oct 9 10:15:52 CEST 2020


El 06/10/2020 a las 01:08, Jean SUZINEAU via fpc-pascal escribió:
>
> In my own code I use BlockRead/BlockWrite, but I'm wondering if I've 
> not seen this somewhere in RTL.
>
> https://github.com/jsuzineau/pascal_o_r_mapping/blob/TjsDataContexte/pascal_o_r_mapping/02_Units/uuStrings.pas 
> <https://github.com/jsuzineau/pascal_o_r_mapping/blob/TjsDataContexte/pascal_o_r_mapping/02_Units/uuStrings.pas>
>
Just nitpicking.
Shouldn't "try" be after the "reset" and after the "rewrite"?
Why don't you allow to write an empty string?

> function String_from_File( _FileName: String): String;
> var
>     F: File;
>     Longueur: Integer;
> begin
>       Result:= '';
>       if not FileExists( _FileName) then exit;
>       AssignFile( F, _FileName);
>       try
>          Reset( F, 1);
>          Longueur:= FileSize( F);
>          if 0 = Longueur then exit;
>          SetLength( Result, Longueur);
>          BlockRead( F, Result[1], Longueur);
>       finally
>              CloseFile( F);
>              end;
> end;
> procedure String_to_File( _FileName: String; _S: String);
> var
>     F: File;
> begin
>       if '' = _S then exit;
>       AssignFile( F, _FileName);
>       try
>          ReWrite( F, 1);
>          BlockWrite( F, _S[1], Length( _S));
>       finally
>              CloseFile( F);
>              end;
> end;
>
>
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


-- 
Saludos

Santiago A.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20201009/42b3b727/attachment.htm>


More information about the fpc-pascal mailing list