Page 1 of 1

GetCurrentDirectory() Strange behaviour

Posted: Wed Feb 21, 2007 3:30 am
by MadMax
GetCurrentDirectory() works well when I run the program from the IDE, but once I compile it; It returns the home directory instead.

Posted: Wed Feb 21, 2007 10:49 am
by Trond
Maybe the home directory is the current one then?

Posted: Wed Feb 21, 2007 10:58 am
by DarkDragon
CurrentDirectory != Exe Directory!!! It's the working directory.

Posted: Wed Feb 21, 2007 2:32 pm
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:

Posted: Wed Feb 21, 2007 4:29 pm
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

Posted: Wed Feb 21, 2007 4:47 pm
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)

Posted: Wed Feb 21, 2007 5:20 pm
by remi_meier
@MadMax: Use GetPathPart(ProgramFilename()) instead.

Posted: Wed Feb 21, 2007 9:01 pm
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.

Posted: Wed Feb 21, 2007 9:40 pm
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.

Posted: Thu Feb 22, 2007 1:45 pm
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.

Posted: Thu Feb 22, 2007 2:20 pm
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.

Posted: Thu Feb 22, 2007 2:23 pm
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)

Posted: Fri Feb 23, 2007 12:03 am
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!!!!

Posted: Fri Feb 23, 2007 7:52 am
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()

Posted: Sun Feb 25, 2007 5:46 pm
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.