Paer Toernell
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
actually, arioh is nearly right
it's
ExtractFilePath( ParamStr( 0 ) ) + 'data\1.txt'
it's
ExtractFilePath( ParamStr( 0 ) ) + 'data\1.txt'
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:Stri ng):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
Function FileToString(FileName:Stri
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')
ASKER
Ustin, a question - does the line:
BlockRead(F,Result[1],FSz) ;
read the whole file? I have 80 mb files....
BlockRead(F,Result[1],FSz)
read the whole file? I have 80 mb files....
ASKER
Thanx for additional answer.
yes, string with whole file will be returned.
It is example only :) but for 80mb it is possible
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
if you use ".\data\1.txt" you will start from GetCurrentDir
use
FileName := IncludeTrailingPathDelimit
as arioh suggested