GetCurrentDirectory() Strange behaviour

Just starting out? Need help? Post your questions and find answers here.
MadMax
Enthusiast
Enthusiast
Posts: 237
Joined: Mon Oct 06, 2003 11:56 am

GetCurrentDirectory() Strange behaviour

Post by MadMax »

GetCurrentDirectory() works well when I run the program from the IDE, but once I compile it; It returns the home directory instead.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Maybe the home directory is the current one then?
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

CurrentDirectory != Exe Directory!!! It's the working directory.
bye,
Daniel
MadMax
Enthusiast
Enthusiast
Posts: 237
Joined: Mon Oct 06, 2003 11:56 am

Post by MadMax »

So what's the point of that command then?
And why should it work from the editor, and not from a compiled version?
And why a redundant "GetHomeDirectory()"?

This IS an issue, as I can hardly ask people that install my programs to only run them from their home directory. If they happen to put them in another folder the program won't work.

What annoys me is that if it works ok from the editor, it really can't be that difficult to fix.

Don't know maybe I'm in a bad mood because some drunken idiot smashed my car, and it's going to take at least 2 months to repair it. But I certainly don't apreciate much the "humourous" replies to my post. :evil:
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

It's not funny. The current directory has absolutely nothing at all to do with where the executable is. Also it simply happens to be the home directory in your case.

http://unixhelp.ed.ac.uk/concepts/currdir.html
http://en.wikipedia.org/wiki/Working_directory
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

Go into a terminal.
Type in "cd ~" + return (your home dir)
Now start your program from there (Don't use cd again!)
The current directory of the program will be your home directory.

Now Type in "cd ../bin/" (or any other directory, just try it out)
Start the program and the current directory is "../bin/" (or whatever you've chosen)
bye,
Daniel
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

@MadMax: Use GetPathPart(ProgramFilename()) instead.
Athlon64 3700+, 1024MB Ram, Radeon X1600
MadMax
Enthusiast
Enthusiast
Posts: 237
Joined: Mon Oct 06, 2003 11:56 am

Post by MadMax »

@remi_meier: Thanks for your sugestion, this works well for the compiled version. Of course it's still a pain when running from the editor as here it returns "/tmp/", but well I can solve this with a couple of code lines.

I STILL think this is a bug.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

> I STILL think this is a bug.
So what is the correct directory it should return when you run it as standalone?

Edit: I tested it, and it's working correctly. I don't understand why you think that the current directory is even remotely related the directory where the executable is. The two have absolutely NOTHING do with each other.
MadMax
Enthusiast
Enthusiast
Posts: 237
Joined: Mon Oct 06, 2003 11:56 am

Post by MadMax »

I borrowed my wife's WindowsXP laptop, and tried from there and it works as expected (or maybe as not) in any case results are the same whether the program is run from the Editor, Shell, Clicked or Linked.

From my PC (Fedora Core 5) It works as expected (by me at least) from the Editor and Shell, but if you click it or link it, then the program will as I don't expect it to(Although you claim it's normal).

The point is, If results are not consistant, like everything will seem to be working OK from the editor, but then once I compile it behaviour will change. This means I need to write 2 versions of the same program, one to test within the editor and the one I want to compile, OK thanks to remi_meier's sugestion I can sort this out. But still I at least would expect the program to behave the same.

Reporting this as a BUG wasn't done with the intention of annoying anyone. If this inconsistancy is OK, well fair enough, but should at least be mentioned in the help files.

NOTE: I'm well aware (now) that this can be because of the differences between Windows and Linux. And I also know that PureBasic is primarily designed for the windows platform. Still PureBasic is AMAZING and mostly very nice to use. THANKS TO ALL THOSE THAT HELPED.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

But the current directory is set by the operating system. Of course the result differs when the current directory differs.

Complain to your favourite linux distro to make them change the behavior of setting the current directory.

In Windows the current directory will be the location of the shortcut if you open the program from one, so if that isn't the case, then something weird is going on.
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

Trond wrote:In Windows the current directory will be the location of the shortcut if you open the program from one, so if that isn't the case, then something weird is going on.
Just if there's no other working directory set in the shortcut (Rightclick->Properties->Shortcut: Working directory or whatever)
bye,
Daniel
MadMax
Enthusiast
Enthusiast
Posts: 237
Joined: Mon Oct 06, 2003 11:56 am

Post by MadMax »

Complain to your favourite linux distro to make them change the behavior of setting the current directory.
Or I could use another programing language.

Anyway this is my last post about the issue, no point in feeding the trolls. You win!!!!
DarkDragon
Addict
Addict
Posts: 2348
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

MadMax wrote:
Complain to your favourite linux distro to make them change the behavior of setting the current directory.
Or I could use another programing language.

Anyway this is my last post about the issue, no point in feeding the trolls. You win!!!!
DAMN, LISTEN TO US, IT'S OS DEPENDENT :? . USE ANOTHER PROGRAMMING LANGUAGE AND YOU GET THE SAME RESULTS OR THE LANGUAGE DOES THE FOLLOWING IN YOUR APP AT THE BEGINNING:

Code: Select all

SetCurrentDirectory(GetPathPart(ProgramFilename()))

;...

Debug GetCurrentDirectory()
bye,
Daniel
Fred
Administrator
Administrator
Posts: 18384
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Hey, please calm down here.

Actually, the "current" directory is the default directory of a program, which is set when it is launched. Some examples:

Create an app and put it in c:\Test\App.exe, which just print the current dir:

Code: Select all

OpenConsole() 
PrintN(GetCurrentDirectory())
Open a shell. Go to c:\. Enter: c:\Test\App.exe. It should print "C:\". Why ? Because it's the directory from which you have launched the app.

Now, go to the C:\Test\ directory. It should display "C:\Test". The current directory is different from the installed program directory.

Now, the "Home" directory is another beast, and is "user" dependent. Each user on the system has a private area (located in "Document And Settings\YourLogin" on Windows and probably on "/home/yourlogin" on Linux (and /Users/yourlogin on OS X for completness ;)).

Therefore, this is not a bug, i hope you get your problem sorted.
Post Reply