Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have a wrote a function that does that. It is no pretty, but it works.</p> <pre><code>function SetFileContent(aFileName: string; aFileContent: string; out aErrorMsg: string; aEncoding: TEncoding = nil; aRecreateFile: Boolean = True): Boolean; var vStream: TFileStream; vCurEncoding: TEncoding; vPreamble, vContent: TBytes; vOffSet: Integer; procedure _SetFileContent(aNewFile: Boolean); begin if aNewFile then begin vStream := TFileStream.Create(aFileName, fmCreate); try vPreamble := aEncoding.GetPreamble; If Length(vPreamble) &gt; 0 then begin vStream.WriteBuffer(Pointer(vPreamble)^, Length(vPreamble)); end; vStream.WriteBuffer(Pointer(vContent)^, Length(vContent)); finally vStream.Free; End; end else begin vStream := TFileStream.Create(aFileName, fmOpenWrite); try vStream.Position := vStream.Size; vStream.WriteBuffer(Pointer(vContent)^, Length(vContent)); finally vStream.Free; end; end; end; begin Result := True; try vContent := BytesOf(aFileContent); vCurEncoding := nil; if aEncoding = nil then begin aEncoding := TEncoding.Default; end; vOffSet := TEncoding.GetBufferEncoding(vContent, vCurEncoding); if (vCurEncoding &lt;&gt; aEncoding) and aRecreateFile then begin vContent := TEncoding.Convert(vCurEncoding, aEncoding, vContent, vOffSet, Length(vContent) - vOffSet); end; if FileExists(aFileName) then begin if aRecreateFile then begin _SetFileContent(True); end else begin _SetFileContent(False); end; end else begin _SetFileContent(True); end; except on E: Exception do begin Result := False; aErrorMsg := 'There was an error while trying to write the string ' + aFileContent + ' in the file ' + aFileName + '. Error: ' + E.Message; end; end; end; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload