- 论坛徽章:
- 0
|
4楼的
回复 #3 zlj0721 的帖子
$HOME是shell里面的环境变量,你的程序是由shell启动的,应该也会继承这个环境变量。但是在C里面取环境变量还是比较麻烦的,取出来还要自己解析,很少有人会这么做。
我尝试了一下,可以在 .profile 中定义
export DWNDIR=$HOME/dwn
就可以执行下面的带$HOME路径的程序了
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp ;
char sFpNm [50];
sprintf(sFpNm ,"%s/%s",(char *)getenv("DWNDIR"),"win");
printf("haha--%s\n",sFpNm );
if ((fp = fopen(sFpNm,"w")) == NULL ){
fprintf(stderr, "Cannot open input the file \n");
return 100;
}
fprintf(fp ,"%s|%s\n","oooooo","qqqqqq");
fclose(fp);
return 0; |
|