Solved

A relative pathname in delphi

Posted on 2008-06-20
8
285 Views
Last Modified: 2012-05-05
I need to adress the file '/data/1.txt' relative to the application. What is the syntax?
0
Comment
Question by:Sunsales
8 Comments
 
LVL 9

Accepted Solution

by:
arioh earned 250 total points
ID: 21829847
try ".\data\1.txt"
or ExtractFilePath( ParamStr( 0 ) ) + "\data\1.txt"
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 21830028
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
 
LVL 17

Expert Comment

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

0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 2

Expert Comment

by:Ustin19
ID: 21837163
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
 

Author Comment

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

read the whole file? I have 80 mb files....
0
 

Author Comment

by:Sunsales
ID: 21842129
Thanx for additional answer.
0
 
LVL 2

Expert Comment

by:Ustin19
ID: 21853842
yes, string with whole file will be returned.
It is example only :) but for 80mb it is possible
0
 
LVL 2

Expert Comment

by:Ustin19
ID: 21853850
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

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question