改變當前的工作目錄,函數 chdir()
#include<unistd.h>
int chdir(const char *path);
作用:
改變當前的工作目錄到path,path為絕對路徑
返回值:
成功完成就返回0 否則返回-1,路徑保持不變 errno代表有錯誤
樣例代碼
char *newDir="/net/gcs/0455506/ras";
int ret=chdir(newDir);
if(ret!=0)
perror("Error:");
else printf("Succeffully changed!\n");
得到當前工作目錄,函數getcwd()
getcwd - get the pathname of the current working directory
#include <unistd.h>
char *getcwd(char *buf, size_t size);
作用:
得到當前的工作目錄放到buf中,得到的目錄為絕對路徑
返回值:
成功完成就返回路徑buf,否則返回NULL指針,errno代表有錯誤
參數說明:
buf為返回時存放路徑的字符指針,size為buf的大小
樣例代碼
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
int main() {
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL)
fprintf(stdout, "Current working dir: %s\n", cwd);
else
perror("getcwd() error");
return 0;
}