Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > get path to directory depending from OS

Reply
Thread Tools

get path to directory depending from OS

 
 
liveline
Guest
Posts: n/a
 
      05-15-2010
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.

 
Reply With Quote
 
 
 
 
liveline
Guest
Posts: n/a
 
      05-15-2010

"Christian Hackl" <(E-Mail Removed)> wrote in message
news:hsmpg9$hra$(E-Mail Removed)-september.org...
> 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.
>
>
> --
> Christian Hackl
> http://www.velocityreviews.com/forums/(E-Mail Removed)
>
> Milano 2008/2009 -- L'Italia chiam, s!

---------

ok, show in C++, if you know.

 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      05-15-2010
On Sat, 2010-05-15, Christian Hackl wrote:
> liveline ha scritto:
>
>> "Christian Hackl" <(E-Mail Removed)> wrote in message
>> news:hsmpg9$hra$(E-Mail Removed)-september.org...
>>> 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 .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a function that can test if a path is in a directory or oneof its sub-directory (recursively)? Peng Yu Python 0 11-06-2009 03:41 AM
physical path to virtual path under virtual directory =?Utf-8?B?SmVmZiBCZWVt?= ASP .Net 4 08-01-2007 02:59 PM
File-Find skips directories with spaces depending on path separator on Windows Matt Garrish Perl Misc 9 01-17-2006 02:12 PM
virtual directory name? or unc directory path? Steve Singer ASP .Net 0 02-15-2005 10:26 PM
Customizing the python search path depending on source directory Peter Schwalm Python 7 04-09-2004 02:58 PM



Advertisments