function getFileName(fileDir : string) : string;
var
fileName : string;
tempNum, i : integer;
tempChar : char;
begin
tempNum := 0;
  for I := Length(fileDir) downto 0 do
  begin
    tempNum := tempNum + 1;
    tempChar := fileDir[I-1];
    if tempChar = '\' then
      break;
  end;

  Result := RightStr(fileDir, tempNum);

end;

It shows that acces violation is in this line:

tempChar := fileDir[I-1];

What does it mean? And why there is a such a problem?

listen this code,I created a form,put one editbox and a button
edit1.text set to 'c:\bp\bin\turbo.exe'
and the button's code can be see below

procedure TForm1.Button1Click(Sender: TObject);
      function getFileName(fileDir : string) : string;
      var
      fileName : string;
      tempNum, i : integer;
      tempChar : char;
      tempname:string;(*store the file name only*)
      begin
            tempNum := 0;
            tempname:='';
            for I := Length(fileDir) downto 0 do
            begin
               tempNum := tempNum + 1;
               tempChar := fileDir[I];
               tempname:=tempchar+tempname;
               if (filedir[i-1] = '\') then break;
           end;
       Result := tempname;
     end;
begin
    showmessage(getfilename(edit1.text));(*show the result*)
 end;
(*result is turbo.exe*)

good luck!
by FlamingClaw 2010.03.17.Hungary.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.