Get the string with a specified number from a text file. The end of the string is a couple of characters CR LF (ASCII #13#13). You can specify an encoding for a file. String enumeration starts from 1.

 

Syntax

function LoadLineFromFile(sFileNameWithPath: string; iLineNumber: integer; iEncodingType: integer; var sText: string): integer;

 

Parameters and return values

Parameter

Type

Value

sFileNameWithPath

string

a name of a text file with a full path to it;

iLineNumber

integer

number of a string you need to read;

iEncodingType

integer

file encoding type, number.

var sText

string

read string. If an error occurs — empty string.

 

Function result

0

string receive successfully;

-1

file does not exist

-2

file has less strings than you specified in the variable iLineNumber;

-3

access error to a file;

-4

string number must be greater than zero.

 

Exampe

const
  FILE_NAME = 'F:\Doc\MyChatJSONprotocol.txt';
  LINE = 67;
var
  iResult: integer;
  s, sLog: string;
begin
  iResult := LoadLineFromFile(FILE_NAME, LINE, 6, s);
  
    case iResult of
       0: sLog := s;
      -1: sLog := 'file not found';
      -2: sLog := 'file has less strings then you specified';
      -3: sLog := 'unknown error';
      -4: sLog := 'line number must be greater than 0';
    end;
    
  mLogScript(sLog, '');  
end.

 

Script work result

[18:07:35] (Log "LoadLineFromFile"): 1. Right after connection from the client's side via TCP on a specified port (by default 2004), the server can display the error and disconnect after 3 seconds.

[18:07:35] (Run "LoadLineFromFile"): Script operation time: 7 ms

[18:07:35] (Run "LoadLineFromFile"): Script done successfully.

 

See also

mlogScript