为什么getenv()会倒退? [英] Why does getenv() work backwards?

查看:67
本文介绍了为什么getenv()会倒退?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么getenv会倒退?当我测试一个env变量时,我必须测试它是否等于,而不是等于获得

所需的结果。正如这段简单的代码所示:


#include< iostream>

#include< cstdlib>

使用namespace std;


const char * cpu = getenv(" PROCESSOR_ARCHITECTURE");


int main()

{

if(cpu!=" x86")

cout<< 有效 << endl;

else

cout<< 无效 <<结束;

系统(暂停);

返回0;

}


如果我使用if(cpu ==" x86"),我会失效,这是错误的,但是如果我使用

if(cpu!=" x86),我会有效,这是正确。就像getenv()或WinXP本身一样,这里的东西是

。这是怎么回事?

在这里?

Why does getenv work backwards? When I''m testing an env variable, I
have to test if its NOT equal to, rather than equal to, to get the
desired result. As evidenced by this simple piece of code:

#include <iostream>
#include <cstdlib>
using namespace std;

const char* cpu=getenv("PROCESSOR_ARCHITECTURE");

int main()
{
if(cpu!="x86")
cout << "Valid " << endl;
else
cout << "Invalid " << endl;
system("PAUSE");
return 0;
}

If I use if(cpu=="x86"), I get invalid, which is wrong, but if I use
if(cpu!="x86), I get valid, which is correct. It''s like something is
backwards here, either with getenv() or WinXP itself. What''s going on
here?

推荐答案

#include< iostream>

#include< cstdlib>

using namespace std;


const char * cpu = getenv(" PROCESSOR_ARCHITECTURE");


int main()

{

if(cpu!=" x86")//这是问题,不要'使用!=来比较

cout<< 有效 << endl;

else

cout<< 无效 <<结束;

系统(暂停);

返回0;


}

#include <iostream>
#include <cstdlib>
using namespace std;

const char* cpu=getenv("PROCESSOR_ARCHITECTURE");

int main()
{
if(cpu!="x86") // this is the problem, don''t use != to compare
cout << "Valid " << endl;
else
cout << "Invalid " << endl;
system("PAUSE");
return 0;

}


Protoman写道:
Protoman wrote:
为什么getenv倒退?当我测试一个env变量时,我必须测试它是否等于,而不是等于获得所需的结果。正如这段简单的代码所示:

#include< iostream>
#include< cstdlib>
使用命名空间std;

const char * cpu = getenv(" PROCESSOR_ARCHITECTURE");



if(cpu!=" x86")
cout< < 有效 <<结束;
其他
cout<< 无效 << endl;
system(PAUSE);
返回0;
}
如果我使用if(cpu ==" x86"),我得到无效,这是错误的,但如果我使用
if(cpu!=" x86),我得到有效,这是正确的。就像getenv()或WinXP本身一样,这就像是向后倾斜的东西。这是怎么回事?
Why does getenv work backwards? When I''m testing an env variable, I
have to test if its NOT equal to, rather than equal to, to get the
desired result. As evidenced by this simple piece of code:

#include <iostream>
#include <cstdlib>
using namespace std;

const char* cpu=getenv("PROCESSOR_ARCHITECTURE");

int main()
{
if(cpu!="x86")
cout << "Valid " << endl;
else
cout << "Invalid " << endl;
system("PAUSE");
return 0;
}

If I use if(cpu=="x86"), I get invalid, which is wrong, but if I use
if(cpu!="x86), I get valid, which is correct. It''s like something is
backwards here, either with getenv() or WinXP itself. What''s going on
here?




这个效果我随便都是随机的。你正在做的是比较两个

指针,即两个地址,而不是他们指向的字符序列



要比较两个char数组,使用strcmp(),或者更好,使用

std :: string,这就是我们所拥有的。


问候,

Matthias



This effect is just random I guess. What you''re doing is comparing two
pointers, that is, two addresses, and not the character sequences
they''re pointing to.
To compare two arrays of char, use strcmp(), or better yet, use
std::string, that''s what we have it for.

Regards,
Matthias




Matthias Kaeppler写道:

Matthias Kaeppler wrote:
Protoman写道:
Protoman wrote:
为什么getenv倒退?当我测试一个env变量时,我必须测试它是否等于,而不是等于获得所需的结果。正如这段简单的代码所示:

#include< iostream>
#include< cstdlib>
使用命名空间std;

const char * cpu = getenv(" PROCESSOR_ARCHITECTURE");



if(cpu!=" x86")
cout< < 有效 <<结束;
其他
cout<< 无效 << endl;
system(PAUSE);
返回0;
}
如果我使用if(cpu ==" x86"),我得到无效,这是错误的,但如果我使用
if(cpu!=" x86),我得到有效,这是正确的。就像getenv()或WinXP本身一样,这就像是向后倾斜的东西。这是怎么回事?
Why does getenv work backwards? When I''m testing an env variable, I
have to test if its NOT equal to, rather than equal to, to get the
desired result. As evidenced by this simple piece of code:

#include <iostream>
#include <cstdlib>
using namespace std;

const char* cpu=getenv("PROCESSOR_ARCHITECTURE");

int main()
{
if(cpu!="x86")
cout << "Valid " << endl;
else
cout << "Invalid " << endl;
system("PAUSE");
return 0;
}

If I use if(cpu=="x86"), I get invalid, which is wrong, but if I use
if(cpu!="x86), I get valid, which is correct. It''s like something is
backwards here, either with getenv() or WinXP itself. What''s going on
here?



这个效果我随便都是随机的。你正在做的是比较两个指针,即两个地址,而不是它们指向的字符序列。
要比较两个char数组,使用strcmp ()或者更好的是,使用
std :: string,这就是我们所拥有的。

问候,
Matthias



This effect is just random I guess. What you''re doing is comparing two
pointers, that is, two addresses, and not the character sequences
they''re pointing to.
To compare two arrays of char, use strcmp(), or better yet, use
std::string, that''s what we have it for.

Regards,
Matthias




不过,我想知道......猜猜我只会想到用getenv()向后思考

。无论如何我可以确保程序只有在运行的机器具有相同的env变量

值作为其编译的机器时才能工作吗? />



Still, I wonder... Guess I''ll just succom to having to think backwards
with getenv(). And is there anyway I can make sure the program will
only work if the machine its running on has the same env variable
values as the machine its compiled on?


这篇关于为什么getenv()会倒退?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆