get path to directory depending from OS

L

liveline

i am looking for code snippet (C not C++) how to get path to target
directory based on common predefined environment variables for Windows XP,
Vista, Windows 7 (and Linux, if possible). First code need determine what
is operating system, them use predefined environment variables to get path
to target directory.
 
L

liveline

Christian Hackl said:
liveline ha scritto:


So why do you post in comp.lang.c++, then? C questions belong to
comp.lang.c.


--
Christian Hackl
(e-mail address removed)

Milano 2008/2009 -- L'Italia chiamò, sì!
 
J

Jorgen Grahn

liveline ha scritto:


OK, so let me reproduce your original message for context:

The task is not well-defined, but you seem to assume it's like walking
$PATH looking for a binary to run, so I do too.
For retrieving the content of environment variables, you can use the
getenv function:

http://www.cplusplus.com/reference/clibrary/cstdlib/getenv/

Here's an example of using it with "PATH":


#include <iostream>
#include <string>
#include <cstdlib>

int main()
{
char const *directory_ptr = getenv("PATH");
if (directory_ptr != 0)
{
std::string const directory = directory_ptr;
std::cout << directory << "\n";
}
else
{
std::cout << "PATH does not exist\n";
}
}


I don't see any need to determine the operating system. Your environment
variable's name is the same, isn't it? (Actually, "PATH" is an example
of one working both in Windows and Linux.)

I think you are confusing the two meanings of "path": the full name of
a file system object (file or directory), or the Unix shell/cmd.exe
list of direcories to search for executables.

Also if you look at the contents of getenv("PATH"), it varies between
OSes. In Unix it's directory names separated by ':', in Windows it's
directory names separated by some other character (I can't remember
which).

Then at some point you probably have to create new paths (the name of
the "bin" directory in "/usr") and check if a directory exists, or
create it. None of those things are standardized in C or C++.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top