- 相關推薦
linux語言中的mkdi函數
mkdir函數用于創建目錄。格式如下:
#include
#include
#include
int mkdir(const char *pathname,mode_t mode);
其中參數pathname是新創建目錄的目錄名,mode指定該目錄的訪問權限,這些位將受到文件創建方式屏蔽(umask)的修正。
該函數創建一個名為pathname的空目錄,此目錄自動含有“.”和“..”2個登記項。這個新創建目錄的用戶ID被設置為調用進程的有效用戶ID,其組則為父目錄的組ID或者進程的有效組ID。
若調用成功,mkdir將更新該目錄的st_atime、st_ctime和st_mtime,同時更新其父目錄的st_ctime和st_mtime,然后返回0。若調用失敗,mkdir將返回-1.
由pathname指定的新目錄的父目錄必須存在,并且調用進程必須具有該父目錄的寫權限以及pathname涉及的各個分路徑目錄的搜尋權限。
rndir函數刪除一個空目錄,它的格式如下:
#include
int rmdir(const char *pathname);
使用rmdir函數時,目錄必須為空,否則調用失敗,函數返回-1.成功時,函數返回0.
例子:創建一個新目錄,然后刪除此目錄。
復制代碼 代碼如下:
#include
#include
#include
#include
int main(int argc,char *argv[])
{
char path[1000];
char file[1000];
/*
if(argc!=2)
{
printf("Usage mk
return 1;
}
*/
argv[1] = "test";
getwd(path); //取得當前工作目錄
printf("current dirctory is:%sn",path);
if(mkdir(argv[1],S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH)<0)//創建新目錄
{
printf("mkdir failedn");
return 2;
}
if(chdir(argv[1])<0) //改變當前工作目錄為新目錄
{
printf("chdir failed n");
return 3;
}
getwd(path);
printf("mkdir successed.n New current directory is:%sn",path);
//rmdir(path); //刪除新建目錄
printf("%s is removedn",path);
return 0;
}
【linux語言中的mkdi函數】相關文章:
C語言中函數的區分08-30
C語言中isalnum()函數和isalpha()函數的對比10-12
C語言中gets()函數知識08-10
C語言中關于時間的函數10-24
C語言中strpbr()函數的用法07-25
c語言中time函數的用法08-27
C語言中關于時間的函數08-19
Linux下精確控制時間的函數07-31
Linux系統調用設備的ioctl函數10-20