<div dir="ltr"><div>Wouldn't it be better to implement it directly as <i>IOUtils.TFile</i> (like Deplhi)?</div><div style="margin-left:40px"><i>class function ReadAllBytes(const Path: string): TBytes; static;<br>class function ReadAllLines(const Path: string): TStringDynArray;overload; static;<br>class function ReadAllLines(const Path: string; const Encoding: TEncoding): TStringDynArray; overload; static;<br>class function ReadAllText(const Path: string): string; overload; inline; static;<br>class function ReadAllText(const Path: string; const Encoding: TEncoding): string; overload; inline; static;</i></div><div>Souce: <a href="http://docwiki.embarcadero.com/Libraries/Sydney/en/System.IOUtils.TFile_Methods">http://docwiki.embarcadero.com/Libraries/Sydney/en/System.IOUtils.TFile_Methods</a></div><div>Then there would be only one way in the RTL for this task and not two once <i>IOUtils</i> (foundation webpage says it's planned anyway) is implemented.</div><div><i></i></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Am Di., 6. Okt. 2020 um 10:12 Uhr schrieb Michael Van Canneyt via fpc-pascal <<a href="mailto:fpc-pascal@lists.freepascal.org">fpc-pascal@lists.freepascal.org</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
<br>
On Mon, 5 Oct 2020, Ryan Joseph via fpc-pascal wrote:<br>
<br>
><br>
><br>
>> On Oct 5, 2020, at 5:08 PM, Jean SUZINEAU via fpc-pascal <<a href="mailto:fpc-pascal@lists.freepascal.org" target="_blank">fpc-pascal@lists.freepascal.org</a>> wrote:<br>
>> <br>
>> In my own code I use BlockRead/BlockWrite, but I'm wondering if I've not seen this somewhere in RTL.<br>
>> <br>
>> <br>
><br>
> This looks good to me what about the concerns raised by Michael? I don't<br>
> know enough about text formats but using AnsiString always just seems to<br>
> work for me (I assume the compiler did something magic behind the scenes).<br>
<br>
No, we don't deal in magic, only bits and bytes :-)<br>
<br>
I added the following functions to the sysutils unit (rev 47056):<br>
<br>
// Read raw content as bytes<br>
<br>
Function GetFileContents(Const aFileName : RawByteString) : TBytes;<br>
Function GetFileContents(Const aFileName : UnicodeString) : TBytes;<br>
Function GetFileContents(Const aHandle : THandle) : TBytes;<br>
<br>
// Read content as string<br>
<br>
// Assume TEncoding.SystemEncoding<br>
Function GetFileAsString(Const aFileName : RawByteString) : RawByteString;<br>
// Specify encoding<br>
Function GetFileAsString(Const aFileName : RawByteString; aEncoding : TEncoding) : RawByteString;<br>
// Assume TEncoding.Unicode contents<br>
Function GetFileAsString(Const aFileName : UnicodeString) : UnicodeString;<br>
// Specify encoding, return Unicode string.<br>
Function GetFileAsString(Const aFileName : UnicodeString; aEncoding : TEncoding) : UnicodeString;<br>
<br>
These functions will raise an exception if the file cannot be opened or read.<br>
They cannot be used on handles that do not support FileSeek() (sockets, pipes, stdin/stdout etc.).<br>
<br>
I did some tests on encoding conversion but not extensively. <br>
If you find any errors, please report them through the bugtracker.<br>
<br>
Michael.<br>
_______________________________________________<br>
fpc-pascal maillist - <a href="mailto:fpc-pascal@lists.freepascal.org" target="_blank">fpc-pascal@lists.freepascal.org</a><br>
<a href="https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal" rel="noreferrer" target="_blank">https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal</a><br>
</blockquote></div>