unix环境高级编程-mkdir和rmdir函数

4.20 mkdir和rmdir函数

#include <sys/stat.h> int mkdir(const char* path,mode_t mode);


成功返回0,失败-1.

此函数创建了一个新的空目录。其中.和..目录项制动创建的。所指定的文件访问的文件访问权限mode由进程的文件模式创建屏蔽字的修改。

查看GNU C

int mkdir (const char *filename, mode t mode) [Function]
The mkdir function creates a new, empty directory with name filename.
The argument mode specifies the file permissions for the new directory file. See
Section 14.9.5 [The Mode Bits for Access Permission], page 370, for more information
about this.
A return value of 0 indicates successful completion, and -1 indicates failure. In
addition to the usual file name syntax errors (see Section 11.2.3 [File Name Errors],
page 224), the following errno error conditions are defined for this function:
EACCES Write permission is denied for the parent directory in which the new
directory is to be added.
EEXIST A file named filename already exists.
EMLINK The parent directory has too many links (entries).
Well-designed file systems never report this error, because they permit
more links than your disk could possibly hold. However, you must still
take account of the possibility of this error, as it could result from network
access to a file system on another machine.
ENOSPC The file system doesn’t have enough room to create the new directory.
EROFS The parent directory of the directory being created is on a read-only file
system and cannot be modified.
To use this function, your program should include the header file ‘sys/stat.h’.

常见的错误是指定与文件相同的mode(只指定读写权限)。但是,对于目录通常至少要设置一个执行权位。以允许访问该目录忠的文件名。

用mkdir可以删除一个一个空目录

如下定义

#include<unistd.h> int rmdir(const char* pathname);

int rmdir (const char *filename) [Function]
The rmdir function deletes a directory. The directory must be empty before it can
be removed; in other words, it can only contain entries for ‘.’ and ‘..’.
In most other respects, rmdir behaves like unlink. There are two additional errno
error conditions defined for rmdir:
ENOTEMPTY
EEXIST The directory to be deleted is not empty.
These two error codes are synonymous; some systems use one, and some use the other.
The GNU system always uses ENOTEMPTY.
The prototype for this function is declared in the header file ‘unistd.h’.
int remove (const char *filename) [Function]
This is the ISO C function to remove a file. It works like unlink for files and like
rmdir for directories. remove is declared in ‘stdio.h’.

。。

如果掉欧诺个此函数使目录的链接计数为0,并且也没有其他进程打开此目录,则释放由此目录占用的空间,如果链接计数为0,有一个或者几个进程打开此目录,则在此函数返回钱删除最后一个链接及.和..目录。另外在此目录中不能再创建新文件。但是在最后一个进程关闭它之前并不释放此目录,

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值