C++ C++;:Setenv()。Visual Studio中未定义的标识符
根据我能在网上找到的所有文档,我的代码似乎是正确的。我的IDE是MS Visual Studio Xpress 4 Windows Desktop 2012,其编译器抛出错误:C++ C++;:Setenv()。Visual Studio中未定义的标识符,c++,visual-c++,visual-studio-2012,environment-variables,setenv,C++,Visual C++,Visual Studio 2012,Environment Variables,Setenv,根据我能在网上找到的所有文档,我的代码似乎是正确的。我的IDE是MS Visual Studio Xpress 4 Windows Desktop 2012,其编译器抛出错误: 错误1错误C3861:'setenv':找不到标识符e:\users\owner\documents\visual studio 2012\projects\project1\project1\source1.cpp 18 1 project1 帮帮我 #include <windows.h> #includ
错误1错误C3861:'setenv':找不到标识符e:\users\owner\documents\visual studio 2012\projects\project1\project1\source1.cpp 18 1 project1
帮帮我
#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);}
};
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int howmanyclass=0;
int main(){
long checklength=sizeof(getenv(“classSize”))/sizeof(*getenv(“classSize”));
if(checklength==0){
不知道多少班;
cin.ignore();
setenv(“classSize”,howmanyclass,1);}
};
尝试\u putenv
而不是setenv
您可以使用以下两种方法之一:将字符串参数作为字符串classSize=7
ostringstream classSize;
classSize << "classSize=" << howManyInClass;
_putenv(classSize.str().c_str());
Microsoft的运行时库不支持标准的
setenv()
函数。您可以使用它们的替代品\u putenv()
,或者,对于可移植代码,我更喜欢使用简单的包装器
这是我的标准接口包装器:
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);
}
遇到链接错误的原因是,如果查看stdlib.h库的内容,您会发现,setenv()没有在其中声明。乍一看,它是一个C标准API,但看起来Windows并没有遵循所有标准。或者,您可以将您的VS配置为使用CRT而不是Windows运行时,在这种情况下,我认为setenv将被识别。等等,我不需要2,因为我已经使用std作为名称空间,这是一个更好的安全解决方案,但请记住,只有在VS2005之后才支持此功能。您能帮我纠正错误:“错误1错误C4996:“getenv”:此函数或变量可能不安全。请考虑使用要禁用弃用,请使用_CRT\u SECURE\u NO\u警告。有关详细信息,请参阅联机帮助。e:\users\owner\documents\visual studio 2012\projects\project1\source1.cpp 12 1 SchoolManagementSystem'Thank
\u putenv()
\u putenv\u s链接似乎已失效。我还尝试在Windows上从processenv.h设置环境变量(名称、值),但我确保它有缺陷。由于某些原因,LoadLibrary()API未看到SetEnvironmentVariable()修改的路径_putenv_s()对我的案子很有效。
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);
}