Solved

A relative pathname in delphi

Posted on 2008-06-20
8
288 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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