A relative pathname in delphi

I need to adress the file '/data/1.txt' relative to the application. What is the syntax?
Paer ToernellAsked:
Who is Participating?
 
ariohConnect With a Mentor Commented:
try ".\data\1.txt"
or ExtractFilePath( ParamStr( 0 ) ) + "\data\1.txt"
0
 
Geert GruwezOracle dbaCommented:
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




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

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Ustin19Commented:
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
0
 
Paer ToernellAuthor Commented:
Ustin, a question - does the line:
  BlockRead(F,Result[1],FSz);

read the whole file? I have 80 mb files....
0
 
Paer ToernellAuthor Commented:
Thanx for additional answer.
0
 
Ustin19Commented:
yes, string with whole file will be returned.
It is example only :) but for 80mb it is possible
0
 
Ustin19Commented:
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
0
All Courses

From novice to tech pro — start learning today.