关于系统函数open(),close(),read(),write(),chdir(),opendir(),readdir(),closedir(),rmdir(),mkdir(),getcwd();

12 篇文章 2 订阅
7 篇文章 0 订阅

1:getcwd()
#include<unistd.h>
char *getcwd(char *buf,size_t size);
该函数的作用是把当前绝对路径复制到参数*buf中,size是*buf的大小。成功返回当前绝对路径地址,否则返回NULL。
2:mkdir()
#include <sys/stat.h>
int mkdir(const char *filename, mode_t mode);
该函数的作用是创建文件夹,*filename是要创建文件的文件名,mode_t mode是创建文件的权限。成功返回0,否则返回-1。
3:rmdir()
#include<unistd.h>
int rmdir(const char *name);
该函数的作用是删除空文件夹,*name是被删除的目录名。成功返回0,否则返回-1。
4:chdir()
#include<unistd.h>
int chdir(const char *path);
该函数的作用是改变目录路径,*path是目标路径,可以是绝对路径或相对路径。成功返回0,否则返回-1。
5:opendir()
#include<sys / types.h>
#include <dirent.h>
DIR *opendir(const char path);
该函数的作用是打开一个目录,*path是被打开的目录名,成功返回DIR*类型的目录流,否则返回NULL。
6:readdir()
#include <sys / types.h>
#include <dirent.h>
struct dirent * readdir(DIR * dir);
结构dirent定义如下
struct dirent {
ino_t d_ino; /
inode number /
off_t d_off; /
offset to the next dirent /
unsigned short d_reclen; /
length of this record /
unsigned char d_type; /
type of file /
char d_name[256]; /
filename */
};
该函数的作用是读目录流为* dir的目录,成功返回struct dirent *类型的指针(下一个目录进入点),否则返回NULL。
7:closedir()
#include<sys/types.h>
#include<dirent.h>
int closedir(DIR * dir);
该函数的作用是关闭目录流*dir,成功返回0,否则返回-1。
8:rename()
#include <stdio.h>
int rename(const char *oldname, const char *newname);
该函数的作用是给文件重命名, *oldname是旧文件名,*newname是新文件名。成功返回0,否则返回-1。
9:open()
#include<fcntl.h>
int open(constchar *pathname,int flags);
int open(constchar *pathname,int flags,mode_t mode);
该函数的作用是打开和创建文件,*pathname是文件名,flags是被打开\创建文件的模式(可选值:O_RDONLY只读模式,O_WRONLY只写模式,O_RDWR读写模式)。mode只在新创建文件时才用到的,该值设置被创建的新文件权限。成功返回文件描述符,否则返回-1.
10:read()
#include<unistd.h>
ssize_t read(int filedes, void *buf, size_t count);
该函数的作用是读文件,filedes是被读文件描述符,*buf是存放被读文件数据的内存,count是要读的字节数。成功返回所读的字节数,读到末尾返回0,出错返回-1。
11:write()
#include <unistd.h>
ssize_t write (int fd,const void * buf,size_t count);
fd是文件描述符,该函数的作用是把* buf存储的内容写count个字节到文件fd中。成功返回写入的字节数,出错返回-1。
12:close()
#include <unistd.h>
int close(int fd)
fd是文件描述符,该函数的功能是关闭文件描述符为fd的文件,成功返回0,否则返回-1;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值