niceguy335
niceguy335

Reputation: 401

Relative Path on f.LoadFromfile and f.SaveToFile

Whats the correct way of using relactive path on those functions? Tried some but only absolute path worked fine. Here is some code sample for illustrate. Thanks!

Procedure test;
Var
f : TStringList;
trash: Cardinal;
TempTime,SumTime : Cardinal;
TimeInterno : TDateTime;
begin
  f := TStringList.Create;
  f.LoadFromfile('D:\test\Ignores.txt');
  for Loop := 0 to f.Count-1 do
  begin
    Ignore(f.Strings[Loop].tointeger);
  end;
  f.Free;
   FindType(-1,trash);

            TimeInterno := Now;
            f := TStringList.Create;
            if not FileExists('D:\test\Ignores.txt') then
            begin
              f.SaveToFile('D:\test\Ignores.txt');
            end;
            Wait(100);
            if FileExists('D:\test\Ignores.txt') then
            begin
              f.SaveToFile('D:\test\Ignores.txt');
              Wait(100);
              f.LoadFromfile('D:\test\Ignores.txt');
            end;
            f.add(IntToStr(findType(-1,Ground)));
            f.SaveToFile('D:\test\Ignores.txt');
            f.Free;
            Ignore(trash);
            Wait(200);
            trash := FindType(-1,Ground);
            UseObject(trash);

Upvotes: 0

Views: 728

Answers (1)

Marco van de Voort
Marco van de Voort

Reputation: 26356

Relative path should be working fine, but make sure what you are assuming to be relative to is alright. It should be relative to the current working directory, which is not always the same as the .exe directory.

On most platforms you can workaround this by adding your relative paths to the exe dir, e.g.

sExePath := ExtractFilePath(paramstr(0));

and then use

 appendtrailingpathdelimiter(sexepath)+yourrelativepathvarorexpression;

Upvotes: 1

Related Questions