Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

A relative pathname in delphi

Posted on 2008-06-20
8
Medium Priority
?
300 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 1000 total points
ID: 21829847
try ".\data\1.txt"
or ExtractFilePath( ParamStr( 0 ) ) + "\data\1.txt"
0
 
LVL 38

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

824 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