get path to directory depending from OS

Discussion in 'C++' started by liveline, May 15, 2010.

  1. liveline

    liveline Guest

    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.
     
    liveline, May 15, 2010
    #1
    1. Advertising

  2. liveline

    liveline Guest

    "Christian Hackl" <> wrote in message
    news:hsmpg9$hra$-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
    >
    >
    > Milano 2008/2009 -- L'Italia chiamò, sì!

    ---------

    ok, show in C++, if you know.
     
    liveline, May 15, 2010
    #2
    1. Advertising

  3. liveline

    Jorgen Grahn Guest

    On Sat, 2010-05-15, Christian Hackl wrote:
    > liveline ha scritto:
    >
    >> "Christian Hackl" <> wrote in message
    >> news:hsmpg9$hra$-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/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

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, May 16, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Singer
    Replies:
    0
    Views:
    552
    Steve Singer
    Feb 15, 2005
  2. Peter Schwalm
    Replies:
    7
    Views:
    359
    Peter Hansen
    Apr 9, 2004
  3. =?Utf-8?B?SmVmZiBCZWVt?=

    physical path to virtual path under virtual directory

    =?Utf-8?B?SmVmZiBCZWVt?=, Aug 1, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    1,068
    Eliyahu Goldin
    Aug 1, 2007
  4. Peng Yu
    Replies:
    0
    Views:
    345
    Peng Yu
    Nov 6, 2009
  5. Matt Garrish
    Replies:
    9
    Views:
    170
    Matt Garrish
    Jan 17, 2006
Loading...

Share This Page