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);
}
-------------------------------------------------------------------------------------