Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Setenv(). Undefined identifier in Visual Studio

Look my code seems to be correct, according to all the documentation I can find online. My IDE is MS Visual Studio Xpress 4 Windows Desktop 2012, and it's compiler is throwing up the error:

Error 1 error C3861: 'setenv': identifier not found e:\users\owner\documents\visual studio 2012\projects\project1\project1\source1.cpp 18 1 Project1.

Help me!!!

#include <windows.h>
#include <sstream>
#include <ostream>
#include <cstdlib>
#include <iostream>
#include <stdlib.h>

using namespace std;

int howManyInClass = 0;
int main(){

long checklength = sizeof(getenv("classSize"))/sizeof(*getenv("classSize"));
if (checklength==0){
    cout<<"Please enter the ammount of students in your class";
    cin>> howManyInClass;
    cin.ignore();
    setenv("classSize", howManyInClass, 1);}

};
like image 883
MWP Avatar asked Jun 23 '13 05:06

MWP


1 Answers

Microsoft's runtime library doesn't support the standard setenv() function. You could use their replacement _putenv() or, for portable code, I prefer to use a simple wrapper.

Here's my wrapper with the standard interface:

int setenv(const char *name, const char *value, int overwrite)
{
    int errcode = 0;
    if(!overwrite) {
        size_t envsize = 0;
        errcode = getenv_s(&envsize, NULL, 0, name);
        if(errcode || envsize) return errcode;
    }
    return _putenv_s(name, value);
}
like image 71
Bill Weinman Avatar answered Sep 24 '22 13:09

Bill Weinman