C++相对路径转换为绝对路径和linux、window移植api

来源:互联网 发布:mysql utf 8 编辑:程序博客网 时间:2024/06/18 12:19
#include <stdio.h>#include <direct.h>#include <stdlib.h>#include <stdio.h>#include  <limits.h>#ifdef _WIN32#include <windows.h>#include <direct.h>#include <io.h>#else#include<unistd.h>#endif#include <iostream>using namespace std;/**linux和window移植api头文件, 函数的不同,可以用如下宏,预处理:#ifdef WIN32  // windows handle#elif __linux__  // linux handle#else  // others handle#endif如果两个函数参数和返回值都相同时,如在windows下没有sleep,会使用宏里面定义的Sleep。在linux下由于定义了sleep这样就不会用到这个宏了,它会调用linux下的sleep函数。#ifndef sleep#define sleep(seconds) (Sleep((seconds)*1000))#endifwindows        替代     linuxlocaltime_s             localtime_rinet_addr               inet_ptonGetTickCount            GetTickCountSleep毫秒               sleep秒_mkdir                  mkdirmemset                  bzero_fullpath               realpath  *///C++相对路径转换为绝对路径和linux、window移植api//http://blog.csdn.net/tujiaw/article/details/7871547int main(){    char* buffer;    if( (buffer = _getcwd( NULL, 0 )) == NULL ) {        perror( "_getcwd error" );    } else {        printf( "Current Dir: %s\n", buffer);        //free(buffer);    }    char currentdir[1024]={0},dir[1024]={0};    sprintf(currentdir,"%s/../../dir/test/",buffer);    printf("%s\n", currentdir);#ifdef _WIN32    _fullpath(dir,currentdir,1024);#else    realpath(currentdir,dir);#endif     printf("dir=%s\n", dir);    char resolved_path[1024];#ifdef _WIN32    _fullpath(resolved_path,"/usr/X11R6/lib/modules/../../include/../",1024);#else    realpath("/usr/X11R6/lib/modules/../../include/../",resolved_path);#endif    printf("resolved_path: %s\n", resolved_path);    return 0;}
 string abs_path(string path) { #ifdef _WIN32      #define max_path 4096      char resolved_path[max_path]={0};     _fullpath(resolved_path,path.c_str(),max_path); #else     //linux release有个坑,需要大点的空间     #define max_path 40960     char resolved_path[max_path]={0};     realpath(path.c_str(),resolved_path); #endif     return string(resolved_path); }


0 0