On Sat, 2010-05-15, Christian Hackl wrote:
> liveline ha scritto:
>
>> "Christian Hackl" <> wrote in message
>> news:hsmpg9$hra$...
>>> liveline ha scritto:
>>>
>>>> i am looking for code snippet (C not C++)
>>> So why do you post in comp.lang.c++, then? C questions belong to
>>> comp.lang.c.
>>>
>>
>> ok, show in C++, if you know.
>
> OK, so let me reproduce your original message for context:
>
> > 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.
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/c...stdlib/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
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .