Link to home
Start Free TrialLog in
Avatar of Paer Toernell
Paer ToernellFlag for Thailand

asked on

A relative pathname in delphi

I need to adress the file '/data/1.txt' relative to the application. What is the syntax?
ASKER CERTIFIED SOLUTION
Avatar of arioh
arioh
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Geert G
the first \ indicates the file is at the root of drive
if you use ".\data\1.txt" you will start from GetCurrentDir

use
FileName := IncludeTrailingPathDelimiter(ExtractFilePath( ParamStr( 0 ) )) + "data\1.txt";
as arioh suggested




actually, arioh is nearly right
it's
ExtractFilePath( ParamStr( 0 ) ) + 'data\1.txt'

Avatar of Ustin19
Ustin19

CreateFile or OpenFile (AssignFile etc is wrappers for it under windows) uses current directory of application (at start it is usually is folder with exe until You call SetCurrentDirectory) to adressing with relative pathes. So, code
Function FileToString(FileName:String):String;
Var F:File of Char;
    Fsz:Integer;
Begin
Result:='';
If FileExists(FileName) then
 Begin
  AssignFile(F,FileName);
  Reset(F);
  Fsz:=FileSize(F);
  SetString(Result,Nil,Fsz);
  BlockRead(F,Result[1],FSz);
  CloseFile(F);
 End;//If
End;//FileToString
calling as
FileToString('data\1.txt') returns string with content of exe-relative located \data\1.txt
Avatar of Paer Toernell

ASKER

Ustin, a question - does the line:
  BlockRead(F,Result[1],FSz);

read the whole file? I have 80 mb files....
Thanx for additional answer.
yes, string with whole file will be returned.
It is example only :) but for 80mb it is possible
and this way is theoretically applicable for less than 2GB files, but for this it is VERY slow and it is danger to obtain EOutOfMemory exception