c_cpp Ç由相对路径获得绝对路径的.c

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp Ç由相对路径获得绝对路径的.c相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>



//由相对路径获得绝对路径,需在调用本函数处释放掉返回值指向的内存
char * get_file_path_absolute(char * file_path_relative)
{

    char * path_absolute = NULL;
    path_absolute = (char *)malloc(sizeof(char)*200);
    memset(path_absolute,0, sizeof(char)*200);



    if(file_path_relative[0] == '/')
    {
        strcpy(path_absolute,file_path_relative);
        return path_absolute;
    }



     getcwd(path_absolute,200);
     strcat(path_absolute, "/");
     path_absolute = strcat(path_absolute,file_path_relative);
     
     return path_absolute;


}

以上是关于c_cpp Ç由相对路径获得绝对路径的.c的主要内容,如果未能解决你的问题,请参考以下文章