加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > 正文

c语言获取当前工作路径的实现代码(windows/linux)

发布时间:2020-12-25 11:51:19 所属栏目:创业 来源:网络整理
导读:Linux 函数名: getcwd 功 能: 取得当前的工作目录 用 法: char *getcwd(char *buf,size_t size); 函数说明: getcwd()会将当前的工作目录绝对路径复制到参数buf所指的内存空间,参数size为buf的空间大

5. 删除目录

int _rmdir( const char *dirname );
// 功 能 : 删除名为dirname的目录.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回0
//     失败返回-1,且设置errno为以下三个值之一:
//      EACCESS  : 权限不允许
//      ENOTEMPTY : dirname不是文件夹;或者该文件夹不空;或
//            者dirname为当前工作文件夹;或者dirname
//            为当根文件夹;
//      ENOENT  : 无该文件或目录

6. 其他操作

int _access( const char *path,int mode );
// 功 能 : 测定文件/目录存取权限.
// 头文件 : #include <io.h>
// 参 数 : path - 文件或者目录
//     mode - 权限设定,其值如下:
//          00 Existence only 
//          02 Write permission 
//          04 Read permission 
//          06 Read and write permission

int _chdrive( int drive );
// 功 能 : 更改当前工作驱动器.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回0
//     失败返回-1
// 注 释 : 参数说明
//      drive =1 : A盘
//      drive =2 : B盘
//      drive =3 : C盘 ...

char* _getdcwd( int drive,char *buffer,int maxlen );
// 功 能 : 获得指定驱动器的当前工作路径.
// 头文件 : #include <direct.h>
// 返回值 : 成功返回指向buffer的pointer
//     失败返回NULL,且设置errno为以下三个值之一:
//      ENODEV 无该设备
//      ENOMEM 内存不够
//      ERANGE 结果超出范围
// 注 意 : 当第一个参数为 NULL 时,该函数设置errno为ERANGE

测试:

// 功 能 : 打印目录path中与模式chRE匹配的所有文件明
// 输 入 : path - 待打印的目录
//     chRE - 要求匹配的正则表达式
static void printDir( const char* path,const char* chRE )
{
  char* chCurPath = getcwd( NULL,0);       // 当前工作目录
  printf("current work path: %sn",chCurPath );
  
  
  int ret = _chdir( path );
  if ( ret < 0 )
  {
    perror( path );
  }


  char* newPath = getcwd( NULL,0 );
  printf("new work path: %sn",newPath);
  free(newPath);


  struct _finddata_t data;
  long hnd = _findfirst( chRE,&data );  // 查找文件名与正则表达式chRE的匹配第一个文件
                       // 返回唯一的搜索句柄
  
  if ( hnd < 0 )
  {
    perror( chRE );
  }
  
  int nRet = (hnd <0 ) ? -1 : 1;
  
  while ( nRet >= 0 )
  {
    if ( data.attrib == _A_SUBDIR ) // 如果是目录
      printf("  [%s]*n",data.name );
    else
      printf("  [%s]n",data.name );
    
    nRet = _findnext( hnd,&data );
  }
  
  _findclose( hnd );   // 关闭当前句柄


  chdir( chCurPath);     // 切换回之前的工作目录
  free( chCurPath );
}

C语言获得当前工作路径和.exe路径

今天在想获得.exe可执行路径的时候,遇到了困难。最后还是解决了。
刚开始用 GetCurrentDirectory(或者_getcwd),发现这只能获得当前工作路径。如果代码中对其他文件夹中的文件操作时,则获得的则不一样了。而我要获得当前exe可执行文件所在的路径。
后来发现是这样的:
我的exe文件所在路径为:
C:UsersJovan YangDesktop论文代码UnAPK&Extract all APIDebugxxx.exe

char exeFullPath[MAX_PATH]={0};
GetModuleFileName(NULL,exeFullPath,MAX_PATH);//得到程序模块.exe全路径
//接下来把xxx.exe文件名去掉,有以下四种:
*strrchr( exeFullPath,'') = 0;//得到C:UsersJovan YangDesktop论文代码UnAPK&Extract all APIDebug
strrchr( exeFullPath,'')[0]= 0;//也是得到C:UsersJovan YangDesktop论文代码UnAPK&Extract all APIDebug
*(strrchr( exeFullPath,'')+1) = 0;//得到C:UsersJovan YangDesktop论文代码UnAPK&Extract all APIDebug

strrchr( exeFullPath,'')[1]= 0;//也是得到C:UsersJovan YangDesktop论文代码UnAPK&Extract all APIDebug

好了,就这样。多找找还是能解决的。这篇文章到此就结束了,基本上很详细的介绍了windows中常见操作。

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读