用C语言获取当前路径

7 篇文章 0 订阅

TC2.0

-------------------------------------------------------------------------------------

函数名: getcurdir
功   能: 取指定驱动器的当前目录
用   法: int getcurdir(int drive, char *direc);

程序例:

#include <dir.h>
#include <stdio.h>
#include <string.h>

char *current_directory(char *path)
{
    strcpy(path, "X:\\");          /* fill string with form of response: X:\ */
    path[0] = 'A' + getdisk();     /* replace X with current drive letter */
    getcurdir(0, path+3);          /* fill rest of string with current directory */
    return(path);
}

int main(void)
{
    char curdir[MAXPATH];

    current_directory(curdir);
    printf("The current directory is %s\n", curdir);

    return 0;
}

-------------------------------------------------------------------------------------

Linux

-------------------------------------------------------------------------------------

函数名: getcwd
功   能: 取得当前的工作目录     
用   法: char *getcwd(char *buf, size_t size); 
  
函数说明: getcwd()会将当前的工作目录绝对路径复制到参数buf所指的内存空间,参数size为buf的空间大小。在调用此函数时,buf所指的内存空间要足够大,若工作目录绝对路径的字符串长度超过参数size大小,则回值NULL,errno的值则为ERANGE。倘若参数buf为NULL,getcwd()会依参数size的大小自动配置内存(使用malloc()),如果参数size也为0,则getcwd()会依工作目录绝对路径的字符串程度来决定所配置的内存大小,进程可以在使用完此字符串后利用free()来释放此空间。   
返回值: 执行成功则将结果复制到参数buf所指的内存空间,或是返回自动配置的字符串指针。失败返回NULL,错误代码存于errno。  

程序例:

#include <stdio.h>     
#include <unistd.h>   

main()   
{   
     char buf[80];   
     getcwd(buf,sizeof(buf));   
     printf("current working directory: %s\n", buf);   
}  

-------------------------------------------------------------------------------------


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值