getenv()?

Discussion in 'C++' started by Protoman, Nov 27, 2005.

  1. Protoman

    Protoman Guest

    What does getenv() do and why would you use it?
     
    Protoman, Nov 27, 2005
    #1
    1. Advertisements

  2. man getenv.

    It returns a pointer to the null terminated string having a value from
    the environment.

    #include <cstdlib>
    #include <iostream>
    int main()
    {
    std::cout << std::getenv("PWD") << std::endl;
    }
     
    Neelesh Bodas, Nov 27, 2005
    #2
    1. Advertisements

  3. Protoman

    S.Colancon Guest

    This function can be used for example because you implement a new algorithm
    and to keep the old algo you can define an envvar that will allow you to
    switch between old and new algo. By this way you define an if(getenv(...))
    to determine which algo will be run.

    Stef
     
    S.Colancon, Nov 27, 2005
    #3
  4. Protoman

    Protoman Guest

    So, what's an enviroment variable and how do I define one?
     
    Protoman, Nov 27, 2005
    #4
  5. Protoman

    Winbatch Guest

    What environment are you in? Windows or *nix?
     
    Winbatch, Nov 27, 2005
    #5
  6. Protoman

    Protoman Guest

    WinXP.
     
    Protoman, Nov 27, 2005
    #6
  7. If you don't know, you don't need to know. It's all system specific, and really has nothing
    to do with C++ (except being able to access it with the getenv function).

    If you really want to know read the system documentation. On Unix, FreeBSD, Linux and similar
    start with the man pages for the shell you use.

    /Niklas Norrthon
     
    Niklas Norrthon, Nov 28, 2005
    #7
  8. Settings/Control Panel/System/Advanced/Environment Variables

    john
     
    John Harrison, Nov 28, 2005
    #8
  9. Protoman

    Protoman Guest

    For example, how would I beable to write a simple program that
    determines the processor the program's executing on? Use WinXP's
    enviroment variable PROCESSOR_ARCHITECTURE?
     
    Protoman, Nov 28, 2005
    #9
  10. Protoman

    Howard Guest

    Please include the text of the message to which you're responding. (I know
    that you've been told -repeatedly - how to do this.)

    To anwer your question: Ask in a windows newsgroup.

    -Howard
     
    Howard, Nov 28, 2005
    #10
  11. PROCESSOR_ARCHITECTURE is not set to anything on my system, (which
    isn't windows), but here is a program that reads the value of
    the environment variable PATH that usually exist both in unix
    and windows environments. I'm sure you are able to modify it to
    suit your needs.

    #include <stdlib.h> /* getenv */
    #include <stdio.h> /* printf */

    int main(void)
    {
    const char* key = "PATH";
    const char* val = getenv(key);
    if (val != NULL) {
    printf("%s=%s\n", key, val);
    }
    else {
    printf("%s not set\n", key);
    }
    return 0;
    }

    Note that getenv returns a char pointer to the value of the
    environment variable. You should not modify it's content, and
    you must not release it's memory. If you wan't to do anything
    with it you should copy it to a buffer that you have control
    over.

    /Niklas Norrthon
     
    Niklas Norrthon, Nov 29, 2005
    #11
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.