Linux高階程式設計基礎——檔案系統程式設計之目錄檔案
阿新 • • 發佈:2018-11-10
檔案系統程式設計之目錄檔案
//1.新建/home/user目錄; //2.把當前工作路徑移至/home/user目錄; //3.列印當前工作路徑;
#include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <dirent.h> #include <errno.h> #include <unistd.h> int main( ) //執行此程式要切換到root使用者 { if (mkdir ("/home/user",0777) < 0) //建立一個新的目錄“/home/use” { perror ("mkdir \n"); printf ("errno is %d \n",errno); } printf ("path : %s \n",getcwd(NULL,55)); //列印當前工作目錄的絕對路徑 chdir ("/home/user"); ////更改當前工作目錄的路徑為("/home/user") printf ("path : %s \n",getcwd(NULL,55)); //再次列印當前工作目錄的絕對路徑 return 0; }