用C/C++程序控制环境变量

In C++, how do i go about using setenv to set the display?  I need to set it like this:

export DISPLAY=0.0

 

1、setenv("DISPLAY",":0.1",1);

 

If you're calling the xrandr functions from your C++ program, then I would expect setenv() should work for you. The 3rd argument of 1 tells setenv() to overwrite any previous value of argument 1 (DISPLAY). You'd be out of luck if xrandr cached the display name though. In that case, you'd have to re-invoke a program from a shell script: the shell script could set the DISPLAY each time before starting your program. That would be less C++ coding

 

 

 

#include <stdlib.h>
#include <stdio.h>

int main(){
char *env1 = getenv("test11");
printf("test11=%s/n", env1); //show current env variable

setenv("test11","abcd",1); //reset it
env1 = getenv("test11");
printf("test11=%s/n", env1);

//this value reset is gone after the program finished

return 0;
}

 

g++ setenv.cpp -o mysetenv

阅读更多
想对作者说点什么? 我来说一句

C例子:获取系统环境变量

2016年04月03日 590B 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭