program TheFile;
function ReadTheFile( PathToFile: String; I: Integer ): String;
var S: String;
F: Integer;
TheArray: Array of String;
begin
F := OpenFile( PathToFile, False );
ReadFileString( F, S, FileSize( F ) );
CloseFile( F );
TheArray := Explode( ';', S );
Result := TheArray[ I ];
end;
function WriteTheFile( PathToFile, NewString: String; I: Integer ): Boolean;
var S, TheString: String;
F, O: Integer;
TheArray: Array of String;
begin
TheString := '';
F := OpenFile( PathToFile, False );
ReadFileString( F, S, FileSize( F ) );
CloseFile( F );
TheArray := Explode( ';', S );
TheArray[ I ] := NewString;
for O := 0 to GetArrayLength( TheArray ) - 1 do
begin
if not ( O = 0 ) then begin
TheArray[ O ] := ';' + TheArray[ O ];
end;
TheString := TheString + TheArray[ O ];
end;
F := RewriteFile( PathToFile, False );
WriteFileString( F, TheString );
CloseFile( F );
Result := True;
end;
begin
Writeln( ReadTheFile( ScriptPath + 'test.txt', 0 ) );
Writeln( ReadTheFile( ScriptPath + 'test.txt', 1 ) );
Writeln( ReadTheFile( ScriptPath + 'test.txt', 2 ) );
WriteTheFile( ScriptPath + 'test.txt', 'usernametest', 0 );
Writeln( ReadTheFile( ScriptPath + 'test.txt', 0 ) );
WriteTheFile( ScriptPath + 'test.txt', 'passwordtest', 1 );
Writeln( ReadTheFile( ScriptPath + 'test.txt', 1 ) );
WriteTheFile( ScriptPath + 'test.txt', 'pintest', 2 );
Writeln( ReadTheFile( ScriptPath + 'test.txt', 2 ) );
end.
{OUTPUT:
Successfully compiled (25 ms)
username
password
pin
usernametest
passwordtest
pintest
Successfully executed
FILE:
username;password;pin
}
The function worked good but i still recommend to use ini files.
E: Added a write function.