C++:Setenv().Visual Studio 中未定义的标识符

根据我可以在网上找到的所有文档,看看我的代码似乎是正确的.我的 IDE 是 MS Visual Studio Xpress 4 Windows Desktop 2012,它的编译器抛出错误:

Error 1 error C3861: 'setenv': identifier not found e:usersownerdocumentsvisual studio 2012projectsproject1project1source1.cpp 18 1 Project1.p>

帮帮我!!!

#include <windows.h>#include <流>#include <ostream>#include <cstdlib>#include <iostream>#include <stdlib.h>使用命名空间标准;int howManyInClass = 0;诠释主要(){长校验长度 = sizeof(getenv("classSize"))/sizeof(*getenv("classSize"));如果(检查长度==0){cout<<"请输入您班级的学生人数";cin>>howManyInClass;cin.ignore();setenv("classSize", howManyInClass, 1);}};

解决方案

您可以使用 _putenv() 将字符串参数作为字符串 classSize=7;

ostringstream classSize;类大小 <<"classSize="<<howManyInClass;_putenv(classSize.str().c_str());

...或(最好)增强安全性 _putenv_s() 将键和值作为单独的 (const char*) 参数;

ostringstream classSize;类大小 <

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:usersownerdocumentsvisual studio 2012projectsproject1project1source1.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);}

};

解决方案

You can either use _putenv() which takes a string parameter as the string classSize=7;

ostringstream classSize;
classSize << "classSize=" << howManyInClass;
_putenv(classSize.str().c_str());

...or (preferably) the security enhanced _putenv_s() that takes the key and the value as separate (const char*) parameters;

ostringstream classSize;
classSize << howManyInClass;
_putenv_s("classSize", classSize.str().c_str());

相关文章