Save text to a file in a specified encoding. If a file existed before, it will be overwritten.

 

Syntax

function SaveTextToFile(sFileName, sText: string; iEncodingType: integer): int64;

 

Parameters and return values

Parameter

Type

Value

sFileName

string

text file name with a full path to it;

sText

string

a content to save to a file;

iEncodingType

integer

file encoding type, number.

 

Function result

Parameter

Meaning

>=0

all OK, file saved successfully; the function returns the size in bytes;

-1

error when saving a file; incorrect name, no place on a disk or another error.

 

Example

The function reads the content of a text file in UTF-8 format from the folder C:\Temp\testmessage.txt, then displays the result in the console using/not using decoding (a file must exist, you can create it manually or download it.


var
  sText: array[1..7] of string;
  sOut: string;
  i, iSize: integer;
begin
  sText[1] := 'There''s a lady who''s sure';
  sText[2] := 'All that glitters is gold';
  sText[3] := 'And she''s buying a stairway to heaven';
  sText[4] := 'When she gets there she knows';
  sText[5] := 'If the stores are all closed';
  sText[6] := 'With a word she can get what she came for';
  sText[7] := 'Oh oh oh oh and she''s buying a stairway to heaven';
  
  sOut := '';
  
    for i := 1 to 7 do sOut := sOut + sText[i] + CRLF;
  
  mLogScript(sOut, '');
  mLogScript('Size: ' + IntToSTr(sOut) + ' bytes', '');
  
  iSize := SaveTextToFile('c:\temp\stairway-to-heaven-utf-16.txt', sOut, 3);
  
  mLogScript('Size on the disk in big-endian UTF-16 format: ' + IntToStr(iSize) + ' bytes', '');
end.


Script work result

[17:28:18] (Log "SaveTextToFile"): There's a lady who's sure

All that glitters is gold

And she's buying a stairway to heaven

When she gets there she knows

If the stores are all closed

With a word she can get what she came for

Oh oh oh oh and she's buying a stairway to heaven

[17:28:18] (Log "SaveTextToFile"): Size: 248 bytes

[17:28:18] (Log "SaveTextToFile"): Size on the disk in big-endian UTF-16 format: 498 bytes

[17:28:18] (Run "SaveTextToFile"): Script operation type: 11 ms

[17:28:18] (Run "SaveTextToFile"): Script done successfully.

 

See also

IntToStr

StrToInt