[Tutor] What is the easiest way to ensure the current working directory is the same as the directory where the main program is saved?

Alan Gauld alan.gauld at yahoo.co.uk
Sat Jul 3 04:31:05 EDT 2021


On 02/07/2021 01:02, boB Stepp wrote:

> One thing that is mildly troubling is that if I wish to write
> cross-platform applications then I must go to the bother of detecting
> the user's OS and stashing "data" appropriately for that OS.  But that
> should not normally be too troublesome.

For industrial grade programs the answer is yes, yes and yes again. OS
approved folder location conventions are different on every major OS:
Windows, MacOS, Linux, VMS, IBM OSxxx etc. Even different variants of
Unix can have different preferred locations for config settings.

Most commercial multi-platform programs will either have a complex
initialization function that detects OS and sets the key folder
locations appropriately or, more common in compiled languages
like C, Java etc, they have a separate platform specific file/module
that sets the locations directly for each OS and then at build time
incorporate the required settings file.

But there is no standard you can follow. To be a good citizen on
each platform you need to do platform specific stuff. And the
preferred location can change between OS versions, especially
on Windows! So you need to use the OS API there to find it.

When you add the possibility of the user having a config that
over-rides the OS default too, it gets even more messy.
It is a non-trivial exercise.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list