<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body smarttemplateinserted="true" text="#000000" bgcolor="#FFFFFF">
<br>
<br>
<br>
<blockquote type="cite"><br>
They cannot be used on handles that do not support FileSeek()
(sockets, pipes, stdin/stdout etc.). <br>
</blockquote>
<br>
<br>
Well, it would be better if it could<br>
<br>
You can just incrementally resize the return array, when reading
succeeds after seeking fails.<br>
<br>
I have a string load function doing that:<br>
<br>
<a class="moz-txt-link-freetext"
href="https://github.com/benibela/bbutils/blob/master/bbutils.pas#L3652">https://github.com/benibela/bbutils/blob/master/bbutils.pas#L3652</a><br>
<br>
<br>
<blockquote type="cite"><br>
<br>
Is it possible to extend this same set of functions to writing?
Naturally I need to write back to the file now and the same
problem
<br>
presents itself. I have to search through other code bases to
find a
<br>
function or Google and find
</blockquote>
<br>
<br>
Writing a file should write the data in a temporary file and then
rename the temporary file to replace the target file. Otherwise it
might destroy the target file, without writing the new content, when
there is an error. Although not even renaming is always safe, some
people say you need a filesystem-specific transaction log.<br>
<br>
<br>
<br>
Benito<br>
<br>
<div class="moz-cite-prefix">On 06.10.2020 10:12, Michael Van
Canneyt via fpc-pascal wrote:<br>
</div>
<blockquote type="cite"
cite="mid:alpine.DEB.2.21.2010060959550.3188@home"> <br>
<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. If
you find any errors, please report them through the bugtracker. <br>
<br>
Michael. <br>
_______________________________________________ <br>
fpc-pascal maillist - <a class="moz-txt-link-abbreviated"
href="mailto:fpc-pascal@lists.freepascal.org">fpc-pascal@lists.freepascal.org</a>
<br>
<a class="moz-txt-link-freetext"
href="https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal">https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal</a>
<br>
</blockquote>
<br>
</body>
</html>