void Platform::get_app_data_dir(std::string& path, const std::string& app_name)
{
const char* home_dir = getenv("HOME"); //get the environment variable , return the pointer
if (!home_dir || strlen(home_dir) == 0) { //dir is null
throw Exception("get home dir failed"); // throw exception
}
path = home_dir;
std::string::iterator end = path.end(); // define the iterator of string
while (end != path.begin() && *(end - 1) == '/') {
path.erase(--end); // delete the symbol '/'
}
path += "/."; // add /.
path += app_name; // add app_name
if (mkdir(path.c_str(), 0700) == -1 && errno != EEXIST) { // dreate the appdata dir
throw Exception("create appdata dir failed"); // S_IRWXU 00700 stand for owner have the authorization of read write operate
} //errno record the system last error code ,whose value is the type of int .
} //#define EEXIST 17 /* File exists */
1. getenv()函数
char *getenv(char *envvar);
函数说明:getenv()用来取得参数envvar
环境变量的内容。参数envvar为
环境变量的名称,如果该变量存在则会返回指向该内容的
指针。
环境变量的格式为envvar=value。getenv函数的返回值存储在一个全局二维数组里,当你再次使用getenv函数时不用担心会覆盖上次的调用结果。
2.fopen()函数
FILE * f
open(const char * path,const char * mode);
参数说明
。。。。。。
http://baike.baidu.com/view/656681.htm
a 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。(EOF符保留)
。。。。。。
3.HOME
返回当前的用户路径
4.mkdir()函数
int mkdir(char * dir, int mode):目录创建函数
运用条件:只能在已存在的目录下建立一级子目录
返回值:
返回0表示成功,返回-1表述出错。
头文件:sys/stat.h
http://baike.baidu.com/view/1279338.htm
http://blog.csdn.net/rabbiteatpumpkin/article/details/6445321
errno parameter defintion
http://baike.baidu.com/view/3485007.htm